List in Python Final PPT.pptx
List in Python Final PPT.pptx
L = ["Eng",65,"Maths",97, "Sc.",89]
Lists are Mutable : Change data in the list 5
List operations – Concatenation 6
L = ["Zen","Ben","Arif","Kity"]
print("Original list", L,sep = ‘\n’) Original list
L.sort() ['Zen', 'Ben', 'Arif', 'Kity']
List in Ascending order
print("List in Ascending order",L,sep = ‘\n’) ['Arif', 'Ben', 'Kity', 'Zen']
L.sort(reverse = True) List in Descending order
print("List in Descending order",L,sep = ‘\n’) ['Zen', 'Kity', 'Ben', 'Arif']
List methods – sort() in nested list 15
L = [[23,12],[34,45],[22,11]]
print("Original list", L,sep = ‘\n’)) Original list [[23, 12], [34,
L.sort() 45], [22, 11]]
List in Ascending order
print("List in Ascending order",L,sep = ‘\n’) [[22, 11], [23, 12], [34, 45]]
L.sort(reverse = True) List in Descending order
print("List in Descending order",L,sep = ‘\n’)) [[34, 45], [23, 12], [22, 11]]
List methods – sort() vs sorted 16
L = [12,9,5,2,3]
print("Original list", L)
Original list [12, 9, 5, 2, 3]
L1 = sorted(L) List in Ascending order [2, 3, 5, 9, 12]
print("List in Ascending order",L1) List in Descending order [12, 9, 5, 3, 2]
L1 = sorted(L,reverse = True)
print("List in Descending order",L1)
List methods – sort() vs sorted 17
L = ["Zen","Ben","Arif","Kity"]
print("Original list", L)
L1 = sorted(L)
print("List in Ascending order",L1)
L1 = sorted(L,reverse = True)
print("List in Descending order",L1)
L = [14,32,29,32,12,24]
print(L) [14, 32, 29, 32, 12, 24]
print("Index of 32 from left",end ="") Index of 32 from left 1
print(L.index(32)) Index of 32 after index 2 3
print("Index of 32 after index 2",end ="") Index of 40 not present
print(L.index(32,2)) ValueError: 40 is not in list
print("Index of 40 not present",end ="")
print(L.index(40))
List built-in functions – min(),max(),sum() 20
max(): It returns the maximum value in the list.
min(): it returns the minimum value in the list.
sum(): it returns the sum of all the values in the list.
L = [14,32,29,32,12,24]
print(L) [14 , 32, 29, 32, 12, 24]
print("Maximum:",max(L)) M aximum: 32
print("Minimum:",min(L)) Mi nimum: 12
print("Sum:",sum(L)) Sum : 143
print("Average:",round(sum(L)/len(L),2)) Av erage: 23.83
Nested List
To access the element of the nested list of list1, we have to specify two indices list1[i][j]. The first
index i will take us to the desired nested list and second index j will take us to the desired
element in that nested list
>>> list1=[1,2,'a','c',[6,7,8],4,9]
>>> list1[4]
[6, 7, 8]
>>> list1[4][1]
7
Copying Lists
The simplest way to make a copy of the list is to assign it to another list.
>>> list1.append(10)
>>> list1
[1, 2, 3, 10]
>>> list2
[1, 2, 3, 10
We can also create a copy or clone of the list as a distinct object by three methods. The first method
uses slicing, the second method uses built-in function list() and the third method uses copy() function of
python library copy.
Method 1
We can slice our original list and store it into a new variable as follows:
newList = oldList[:]
Example
>>> list1 = [1,2,3,4,5]
>>> list2 = list1[:]
>>> list2
[1, 2, 3, 4, 5]
Method 2 Method 3
We can use the built-in function list() as follows: We can use the copy () function as follows:
import copy #import the library copy
newList = list(oldList) #use copy()function of library copy
newList = copy.copy(oldList)
Example
Example
>>> import copy
>>> list1 = [10,20,30,40]
>>> list1 = [1,2,3,4,5]
>>> list2 = list(list1)
>>> list2 = copy.copy(list1)
>>> list2 [10, 20, 30, 40]
>>> list2 [1, 2, 3, 4, 5]
Program 1: To input number of marks to be entered, enter
marks and add in a list and display the average marks.
#Write a program to find the number of times an element occurs in the list
L=eval(input('Enter a list '))
num=eval(input('Enter the number to be searched '))
c=L.count(num)
print('Number ',num, ' occurs ',c, ' number of times')