Methods of List in Python:
What is List in Python?
A list is a sequence datatype. Elements of list are enclosed in square brackets separated by comma. Elements of a list can be of any type. for example :
L = [1, 3, 5, ‘python’, ‘game’]
The above list has five elements and are enclosed in square brackets(separated by comma)
Methods of List (Built-in Functions)
Python offers various methods of list (built in functions) to perform operations on List. Lets discuss one by one
append( ):
This function add a single element at the end of the list. for example
L = [34, 45, 56, 23, 12]
L.append(77)
print(L)
OUTPUT : [34, 45, 56, 23, 12, 77]
L.append(28,9) will return type error as append function can take only one argument
This function can add a list at the end of another list. for example
L.append([28, 29])
OUTPUT : [34, 45, 56, 23, 12, 77, [28, 29] ]
Q1. Write a program to accept five numbers from the user and add in the given list. L = [34, 23, 12]
Ans. L = [34, 23, 12] k = 0 for i in range(5): k = int(input("Enter any number")) L.append(k) print("List after appending is: ", L) Execution of the program Enter any number : 2 Enter any number : 3 Enter any number : 5 Enter any number : 6 Enter any number : 9 List after appending is: [34, 23, 12, 2, 3, 5, 6, 9]
Q2. Write a program to accept 10 numbers from the user and if the number is even then add the elements in list L1 otherwise add in list L2.
Ans. L1 = [ ] L2 = [ ] for i in range(10): k = int(input("Enter any number : ")) if k%2==0: L1.append(k) else: L2.append(k) print("Even number List :",L1) print("Odd number List :",L2) Execution of the program: Enter any number : 1 Enter any number : 2 Enter any number : 3 Enter any number : 4 Enter any number : 5 Enter any number : 6 Enter any number : 7 Enter any number : 8 Enter any number : 9 Enter any number : 10 Even number List : [2, 4, 6, 8, 10] Odd number List : [1, 3, 5, 7, 9]
Methods of List in Python:
extend( )
This function adds all the elements of one list at the end of the other list. for example :
L1 = [1, 6, 3, 9]
L2 = [12, 13, 18, 19]
L1.extend(L2)
print(L1)
Output : [1, 6, 3, 9, 12, 13, 18, 19]
All the elements of list L2 are added at the end of the list L1
Q3. Write the output of the following two coding:
L1 = [1, 2, 3] L2 = [7, 8 ,9] L1.append(L2) print(L1) OUTPUT : [1, 2, 3, [7, 8, 9]]
L1 = [1, 2, 3] L2 = [7, 8 ,9] L1.extend(L2) print(L1) OUTPUT : [1, 2, 3, 7, 8, 9]
insert( ):
This function help us to add an element at a specific index value. for example
L1 = [23, 12, 45, 32]
L1.insert(3, 77) # This statement will add element 77 at index value 3 ie position number 4
print(L1)
OUTPUT : [23, 12, 45, 77, 32]
L1.insert(1, 91) # This statement will add element 91 at index value 1.
print(L1)
OUTPUT : [23, 91, 12, 45, 77, 32]
reverse( ) :
This function simply reverse the order of all the elements in the list. for example :
L1 = [23, 12, 45, 32]
L1.reverse( )
print(L1)
OUTPUT : [32, 45, 12, 23] #The given list is reversed.
#This functions makes the changes in the original list. It does not create the new list.
len( ):
This function returns the length of the list. for example
L1 = [12, 34, 43, 23, 78, 90, 1]
print(len(L1))
OUTPUT : 7
L2 = [“Sumit”, “Naman”, “Parth”, 76, 90, “Mini”]
print(len(L2))
OUTPUT : 6
sort( ):
This function arrange all the elements in increasing order (by default). for example
L1 = [12, 34, 43, 23, 78, 90, 1]
L1.sort( )
print(L1)
OUTPUT : [1, 12, 23, 34, 43, 78, 90]
L2 = [“Sumit”, “Naman”, “Parth”, 76, 90, “Mini”]
L2.sort( )
print(L2)
OUTPUT : TypeError: ‘<‘ not supported between instances of ‘int’ and ‘str’
L3 = [“Sumit”, “Naman”, “Parth”, “Mini”]
L3.sort( )
print(L3)
OUTPUT : [‘Mini’, ‘Naman’, ‘Parth’, ‘Sumit’] # This function arranges String elements according to their ASCII value.
To arrange elements in decreasing/descending order.
L1 = [12, 34, 43, 23, 78, 90, 1]
L1.sort( reverse = True)
print(L1)
OUTPUT : [90, 78, 43, 34, 23, 12, 1]
OR
L1 = [12, 34, 43, 23, 78, 90, 1]
L1.sort( )
L1.reverse( )
print(L1)
OUTPUT : [90, 78, 43, 34, 23, 12, 1]
count( ) :
This function returns the frequency of an element in the list. In Other words, we can say that this function returns how many times an element has occurred in the list. for example
L1 = [1, 2, 5, 2, 6, 1, 7, 9, 1]
print(L1.count(1))
OUTPUT : 3
print(L1.count(2))
OUTPUT : 2
clear( ):
This function remove all the elements of the list. for example
L1 = [1, 2, 5, 2, 6, 1, 7, 9, 1]
L1.clear( )
print(L1)
OUTPUT : [ ]
DELETION OPERATION : Methods of List
pop( ):
This function delete/remove the element from the specified index. This function also return the deleted element. for example
L1 = [90, 56, 87, 98, 23, 6, 78]
print(L1.pop(3 )) #Output of this statement shows that it return the element.
print(L1)
OUTPUT:
98
[90, 56, 87, 23, 6, 78]
NOTE : If we don’t give any index value in pop( ) function then it will delete the last element.
L1 = [90, 56, 87, 98, 23, 6, 78]
print(L1.pop( )) #Output of this statement shows that it return the element.
print(L1)
OUTPUT :
78
[90, 56, 87, 98, 23, 6]
L1 = [“Amit”, “Sumit”, “Naman” , “Manan”, “Kapil”]
print(L1.pop(-2)) # pop( ) function work with negative index value also.
print(L1)
OUTPUT :
Manan
[‘Amit’, ‘Sumit’, ‘Naman’, ‘Kapil’]
del Statement :
The del statement delete the element from the specified index. This statement does not return the element. for example
L1 = [90, 56, 87, 98, 23, 6, 78]
del L1[3]
print(L1)
OUTPUT : [90, 56, 87, 23, 6, 78]
More than one elements can be deleted by using del statement. for example
L1 = [90, 56, 87, 98, 23, 6, 78]
del L1[2 : 5] # will delete three elements of index value 2, 3 and 4
print(L1)
OUTPUT : [90, 56, 6, 78]
Q. What is the difference between pop( ) function and del statement?
Ans.
pop( ) | del statement |
This function return the deleted element | This statement does not return the deleted element |
One element can be deleted at one time | Multiple elements can be deleted. for example del L1[2:5] will delete 3 elements from list L1 |
Index value of the element to be deleted is written in parenthesis. for example L1.pop(2) | Index value of the element to be deleted is written in Square brackets. for example del L1[2] |
remove( ):
This function delete the first occurrence of specified element. This function is used when we know the element to be deleted but not the index value. for example:
L1 = [90, 56, 87, 98, 23, 6, 78, 98]
L1.remove(98) #will remove element 98 from the list which is at index value 3
print(L1)
OUTPUT : [90, 56, 87, 23, 6, 78, 98]
Q. Write a program to remove all even numbers from the list.
Ans. L1 = [90, 56, 87, 98, 23, 6, 78, 98] l = len(L1) i=0 while i <l: if L1[i]%2==0: del L1[i] l=l-1 i=i-1 i = i+1 print(L1)
Searching : Methods of List :
Elements in the list can be searched using index( ) method.
index( ):
This function simply returns the index value of the specified element. for example
L1 = [90, 56, 87, 98, 23, 6, 78, 98]
print(L1.index(87))
OUTPUT : 3
If we specified an element which is not present in the list, then it will return an error. for example
L1 = [90, 56, 87, 98, 23, 6, 78, 98]
print(L1.index(77))
OUTPUT : ValueError: 77 is not in list
Q4. Write a function indv(L, n) which takes a list and a number as an argument and return the index value of number passed as an argument.
Ans. L1 = [23, 12, 45, 32,12] def indv(L, n): if n in L: print(L.index(n)) else: print("number not available in list") indv(L1, 45) OUTPUT : 2
Methods of List
Finding Largest and Smallest Value
max( ) :
This function returns the largest value from the list. for example
L1 = [90, 56, 87, 98, 23, 6, 78, 98]
print(max(L1))
OUTPUT : 98
L1 = [“Amit”, “Sumit”, “Naman” , “Manan”, “Kapil”]
print(max(L1)) # For character value, it checks according to ASCII Value.
OUTPUT : Sumit
min( ) :
This function return the smallest value from the list. for example
L1 = [90, 56, 87, 98, 23, 6, 78, 98]
print(min(L1))
OUTPUT : 6
L1 = [“Amit”, “Sumit”, “Naman” , “Manan”, “Kapil”]
print(min(L1)) # For character value, it checks according to ASCII Value.
OUTPUT : Amit
Disclaimer : I tried to give the correct statement of all the above Methods of List, but if you feel that the code or statement any of the above methods of list, you can share your feedback on csiplearninghub@gmail.com.
Important Link
10 Important Questions of Text File Handling in Python
10 Important Questions of Binary File Handling in Python
Handout of Text File Handling in Python
40+ Important Python File Handling Practice Questions
Handout of Binary File Handling in Python
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series