List 15-01-2025 - Jupyter Notebook
List 15-01-2025 - Jupyter Notebook
List
Earlier when discussing strings we introduced the concept 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)
In [4]: 1 len(my_list)
Out[4]: 7
List Indexing
Indexing work just like in strings.
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))
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
---------------------------------------------------------------------------
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))
List Slicing
We can use a : to perform slicing which grabs everything up to a designated point.
In [ ]: 1 print(colors)
In [ ]: 1 print(colors[2:7:2]) # [start:end:step]
In [ ]: 1 print(colors[::])
In [ ]: 1 print(colors[::-1])
In [ ]: 1 print(colors[-10::])
list operations
... list are mutable(can be change). lets see how can we change the element of the list
In [19]: 1 print(colors)
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
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
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[17]: [5.5, 4, 2]
In [22]: 1 print(li1)
In [24]: 1 sorted(li1,reverse=True)
In [26]: 1 sorted(li1)
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 [31]: 1 print(my_list)
In [33]: 1 my_list.append(2.73)
2 print(my_list)
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]]
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
[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'
[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.
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']
[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 [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
Out[73]: [8, 5, 3, 1, 1, 1, 3, 2, 1]
[1, 2, 3, 1, 1, 1, 3, 5, 8]
1 # Split()
2
3
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)
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)
In [101]: 1 new_list[0][1]
Out[101]: 2
In [103]: 1 new_list[1]
In [104]: 1 new_list[1][2:4]
Out[104]: ['Z']