0% found this document useful (0 votes)
17 views6 pages

List 15-01-2025 - Jupyter Notebook

The document provides an overview of lists in Python, explaining their mutable nature and how they can be created, indexed, and sliced. It covers various list operations and functions, including appending, inserting, extending, and removing elements, as well as methods for finding minimum, maximum, and sorted values. Additionally, it discusses list methods like clear, count, index, and reverse, along with examples demonstrating their usage.

Uploaded by

nirajjoshi072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

List 15-01-2025 - Jupyter Notebook

The document provides an overview of lists in Python, explaining their mutable nature and how they can be created, indexed, and sliced. It covers various list operations and functions, including appending, inserting, extending, and removing elements, as well as methods for finding minimum, maximum, and sorted values. Additionally, it discusses list methods like clear, count, index, and reverse, along with examples demonstrating their usage.

Uploaded by

nirajjoshi072003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1/15/25, 11:04 AM list 13-01-2025 - Jupyter Notebook

List
Earlier when discussing strings we introduced the concept of a sequence in Python.

Lists can be thought of the most general version of a sequence in Python.

Unlike strings, they are mutable, meaning the elements inside a list can be changed!

Lists are constructed with brackets [] and commas separating every element in the list.

In [1]: 1 my_list=[1,2,3,4,5]
2 print(my_list)

[1, 2, 3, 4, 5]

In [2]: 1 type(my_list)

Out[2]: list

In [3]: 1 my_list=[1,2,"lion",0.78,True,6,9]
2 print(my_list)

[1, 2, 'lion', 0.78, True, 6, 9]

In [4]: 1 len(my_list)

Out[4]: 7

List Indexing
Indexing work just like in strings.

A list index refers to the location of an element in a list.

Remember the indexing begins from 0 in Python.

The first element is assigned an index 0, the second element is assigned an index of 1 and so on and so forth.

In [5]: 1 colors=["red","green","blue","purple","black","white","orange","pink"]
2 print(colors)
3 print() # for gap only
4 print(len(colors))

['red', 'green', 'blue', 'purple', 'black', 'white', 'orange', 'pink']

In [6]: 1 print(colors[0])

red

In [7]: 1 print(colors[-2])

orange

In [8]: 1 print(colors[5])

white

In [9]: 1 print(colors[-5])

purple

In [10]: 1 # write a program to create a blank list


2 ​
3 print(type(li))

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [10], in <cell line: 3>()
1 # write a program to create a blank list
----> 3 print(type(li))

NameError: name 'li' is not defined

List Slicing
We can use a : to perform slicing which grabs everything up to a designated point.

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list 13-01-2025.ipynb# 1/6


1/15/25, 11:04 AM list 13-01-2025 - Jupyter Notebook
The starting index is specified on the left of the : and the ending index is specified on the right of the : .

Remember the element located at the right index is not included.

In [ ]: 1 print(colors)

In [ ]: 1 print(colors[2:7:2]) # [start:end:step]

In [ ]: 1 print(colors[0:1:]) # first word

In [ ]: 1 print(colors[::])

In [ ]: 1 print(colors[::-1])

In [ ]: 1 # last 3 element of string


2 ​
3 print(colors[-3::])

In [ ]: 1 # 10 index in not in the range of the list therefore is giving error


2 ​
3 # 10 element of the lise
4 ​
5 print(colors[10]) # 10 index in not in the range of the list therefore is giving error

In [ ]: 1 print(colors[-10::])

list operations
... list are mutable(can be change). lets see how can we change the element of the list

we can also use + to concatenate the list.

In [19]: 1 print(colors)

['red', 'green', 'blue', 'purple', 'black', 'white', 'orange', 'pink']

In [ ]: 1 colors=colors+["yellow","maroon"]
2 ​
3 print(colors)

In [ ]: 1 colors=colors+["lime","gray"]
2 ​
3 print(colors)

In [ ]: 1 print(colors*3)

In [ ]: 1 print(len(colors))

list Functions
#length function

len() function returns the length of our list

In [ ]: 1 print(len(colors))

Min
min()===>return the minimum element in the list, works only for list containing similar datatype

In [12]: 1 li=[2,4,5.5]
2 ​
3 min(li)

Out[12]: 2

In [20]: 1 li1=["M","A","Z",]
2 min(li1)

Out[20]: 'A'

max
max()===> will return a maximum element in the list. containing similar datatype

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list 13-01-2025.ipynb# 2/6


1/15/25, 11:04 AM list 13-01-2025 - Jupyter Notebook

In [ ]: 1 max(li1)

In [ ]: 1 max(li)

In [ ]: 1 print(colors)

In [ ]: 1 max(colors)

In [ ]: 1 min(colors)

In [ ]: 1 print(colors)

15-01-2025

Sorted
sorted()==> returns the sorted list take a reverse boolean as an argument, sorted function works with similar data type

In [14]: 1 print(li)

[2, 4, 5.5]

In [16]: 1 sorted(li)

Out[16]: [2, 4, 5.5]

In [17]: 1 sorted(li,reverse= True) # (__,reverse=True) ha syntax ahe

Out[17]: [5.5, 4, 2]

In [22]: 1 print(li1)

['M', 'A', 'Z']

In [24]: 1 sorted(li1,reverse=True)

Out[24]: ['Z', 'M', 'A']

In [26]: 1 sorted(li1)

Out[26]: ['A', 'M', 'Z']

List Method
If you are familiar with another programming language, you might start to draw parallels between arrays in another language and lists in Python. Lists
in Python however, tend to be more flexible than arrays in other languages for a two good reasons: they have no fixed size (meaning we don't have to
specify how big a list will be), and they have no fixed type constraint (like we've seen above).

Let's go ahead and explore some more special methods for lists:

append()
Use the append() method to permanently add an item to the end of a list.

append() method takes the element which you want to add to the list as an argument

In [28]: 1 my_list= [1,2,3,1,1,1,3,10,5,8]

In [30]: 1 my_list.append("New item") # syntax

In [31]: 1 print(my_list)

[1, 2, 3, 1, 1, 1, 3, 10, 5, 8, 'New item', 'New item']

In [33]: 1 my_list.append(2.73)
2 print(my_list)

[1, 2, 3, 1, 1, 1, 3, 10, 5, 8, 'New item', 'New item', 2.73, 2.73]

In [37]: 1 my_list.append([1,2,3])
2 print(my_list)

[1, 2, 3, 1, 1, 1, 3, 10, 5, 8, 'New item', 'New item', 2.73, 2.73, [1, 2, 3], [1, 2, 3]]

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list 13-01-2025.ipynb# 3/6


1/15/25, 11:04 AM list 13-01-2025 - Jupyter Notebook

In [39]: 1 print(len(my_list)) # appended a list data type in a original list therefore it is given as +1

16

In [41]: 1 my_list.append([10,[19,20],30])
2 print(my_list)

[1, 2, 3, 1, 1, 1, 3, 10, 5, 8, 'New item', 'New item', 2.73, 2.73, [1, 2, 3], [1, 2, 3], [10, [19, 20], 30], [10, [19,
20], 30]]

insert():
inserts the new element at a specific index, takes argument as index no and element

In [44]: 1 my_list.insert(10,30) # syntax( position, no or character you want)


2 print(my_list)

[1, 2, 3, 1, 1, 1, 3, 10, 5, 8, 30, 30, 30, 'New item', 'New item', 2.73, 2.73, [1, 2, 3], [1, 2, 3], [10, [19, 20], 3
0], [10, [19, 20], 30]]

Extend():
extend() Use the extend() method to merge a list to an existing list

extend() method takes a list or any iterable(don't worry about it now) as an argument.

Quite helpful when you have two or more lists and you want to merge them together

In [46]: 1 my_list.extend(["wubba","lubba","dubba"])
2 print(my_list)

[1, 2, 3, 1, 1, 1, 3, 10, 5, 8, 30, 30, 30, 'New item', 'New item', 2.73, 2.73, [1, 2, 3], [1, 2, 3], [10, [19, 20], 3
0], [10, [19, 20], 30], 'wubba', 'lubba', 'dubba', 'wubba', 'lubba', 'dubba']

In [48]: 1 print(len(my_list))

27

1 # POP
2 ​
3 pop()
4 Use pop() to "pop off" an item from the list. (use for remove the item)(and jar specify nahi kela tar last wala
udnar)
5 ​
6 By default pop() takes off the element at the last index, but you can also specify which index to pop off.
7 ​
8 pop() takes the index as an argument and returns the elenent which was popped off.

In [50]: 1 my_list.pop()

Out[50]: 'lubba'

In [52]: 1 print(my_list) # pop takes index as an argument

[1, 2, 3, 1, 1, 1, 3, 10, 5, 8, 30, 30, 30, 'New item', 'New item', 2.73, 2.73, [1, 2, 3], [1, 2, 3], [10, [19, 20], 3
0], [10, [19, 20], 30], 'wubba', 'lubba', 'dubba', 'wubba']

In [54]: 1 my_list.pop(10)

Out[54]: 30

1 # Remove
2 ​
3 ### <code>remove()</code>
4 ​
5 ​
6 * Use <code>remove()</code> to remove an item/element from the list.
7 ​
8 ​
9 ​
10 ​
11 * By default <code>remove()</code> removes the specified element from the list.
12 ​
13 ​
14 ​
15 ​
16 * <code>remove()</code> takes the element as an argument.

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list 13-01-2025.ipynb# 4/6


1/15/25, 11:04 AM list 13-01-2025 - Jupyter Notebook

In [56]: 1 my_list.remove(1)
2 print(my_list)

[2, 3, 1, 1, 3, 10, 5, 8, 30, 'New item', 'New item', 2.73, 2.73, [1, 2, 3], [1, 2, 3], [10, [19, 20], 30], [10, [19, 2
0], 30], 'wubba', 'lubba', 'dubba', 'wubba']

In [59]: 1 my_list.remove("New item")


2 print(my_list)

[2, 3, 1, 1, 3, 10, 5, 8, 30, 2.73, 2.73, [1, 2, 3], [1, 2, 3], [10, [19, 20], 30], [10, [19, 20], 30], 'wubba', 'lubb
a', 'dubba', 'wubba']

Clear
clear()===> empty your list

In [61]: 1 my_list.clear()
2 print(my_list)

[]

Count
count() method returns the occurance of specific element

In [63]: 1 my_list= [1,2,3,1,1,1,3,5,8]

In [65]: 1 my_list.count(1)

Out[65]: 4

1 # Index()
2 ​
3 index() method returns the index of specific element

In [67]: 1 my_list.index(2)

Out[67]: 1

In [69]: 1 my_list.index(1)

Out[69]: 0

In [71]: 1 my_list.index(8)

Out[71]: 8

reverse()
reverse() method reverse the list

In [73]: 1 my_list[::-1] # temporatry

Out[73]: [8, 5, 3, 1, 1, 1, 3, 2, 1]

In [75]: 1 my_list.reverse() # permanent


2 print(my_list)

[1, 2, 3, 1, 1, 1, 3, 5, 8]

1 # Split()
2 ​
3 ​

In [77]: 1 string= "i am data-scientist"


2 print(string.split("-"))

['i am data', 'scientist']

In [80]: 1 print(string.split(" "))

['i', 'am', 'data-scientist']

Another way to update elements in the list

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list 13-01-2025.ipynb# 5/6


1/15/25, 11:04 AM list 13-01-2025 - Jupyter Notebook

In [82]: 1 li=[2,3,5,6,8,9]
2 print(li)

[2, 3, 5, 6, 8, 9]

In [84]: li[4]=40
print(li)

[2, 3, 5, 6, 40, 9]

In [86]: 1 li[0],li[1],li[2]=100,101,102
2 print(li)

[100, 101, 102, 6, 40, 9]

nested list
In [99]: 1 li=[1,2,3]
2 li2=["b","a","c","d"]
3 li3= [4,5,6]
4 ​
5 # make a new list (matrix) from existing 3 list
6 ​
7 new_list=[li,li1,li2]
8 print(new_list)

[[1, 2, 3], ['M', 'A', 'Z'], ['b', 'a', 'c', 'd']]

In [101]: 1 new_list[0][1]

Out[101]: 2

In [103]: 1 new_list[1]

Out[103]: ['M', 'A', 'Z']

In [104]: 1 new_list[1][2:4]

Out[104]: ['Z']

localhost:8888/notebooks/Desktop/NHANU/python/python jupitor/list 13-01-2025.ipynb# 6/6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy