List Functions
List Functions
List Functions/Methods
1) len()
This function returns the length of the list. The length of the list is equal to the
number of elements present in the list.
>>> l = ['Mon','Tue','Wed','Thurs','Fri','Sat']
>>> print(len(l))
6
>>> l=[['Mon','Tue','Wed'],['Thu','fri'],'Sat','Sun']
>>> print(len(l))
4
2) list()
This function will convert the passed parameters into list.
>>> L = list("XIICS2024-25")
>>> L
[‘X’,’I’,’I’,’C’,’S’,’2’,’0’,’2’,’4’,’-‘,’2’,’5’]
3) index()
This method is used to find first index position of a value in a list. It returns-1 if value
is not found in the list.
>>> l=[11,22,27,34,67,98]
>>> l.index(27)
2
>>> L1=[13,18,11,16,18,14]
>>> print(L1.index(18))
1
4) append() :
This method is used to add a new element at the end of list.
>>> L1=[13,18,11,16,18,14]
>>> L1.append(100)
>>> print(L1)
[13, 18, 11, 16, 18, 14, 100]
>>> L1=[13,18,11,16,18,14]
>>> L1. Append([12,14])
>>> print(L1)
[1,3,5,10,[12,14]]
5) extend()
It is used to append elements of another list at the end of existing list.
>>>L1=[‘a’,’b’,’c’,]
>>>L2=[‘d’,’e’]
>>>L1.extend(t2)
>>> print(L1)
[‘a’,’b’,’c’,’d’,’e’]
6) insert()
The insert () method is used to insert an element at any position in a list.
>>> L1=[13,18,11,16,18,14]
>>> L1.insert(1,100) #insert 100 at index 1 i.e. second element.
>>> print(L1)
[13, 100, 18, 11, 16, 18, 14]
>>>L1=[13,18,11,16,18,14]
>>>L1.insert(-2,100)
[13, 18, 11, 16, 100, 18, 14]
7) pop()
It is used to remove and return an element from the given index in the list. If no
index is specified, pop() removes and returns the last item in the list.
>>> L1=[13,18,11,16,18,14]
>>> val=L1.pop() #Deletes last element of list and stores in val.
>>> print(val)
14
>>> L1=[13,18,11,16,10,14]
>>> val=L1.pop(1) #element at index 1 i.e. 18 is deleted and stored in val
>>> print(val)
18
8) remove()
It removes the first occurrence of an item from the list.
>>> L1=[13,18,11,16,18,14]
>>> L1.remove(11)
>>> print(L1) #List will not contain 11
[13, 18, 16, 18, 14]
9) clear()
This method is used to remove all the items from a list and make the list empty. It
doesn’t delete the list permanently.
>>> L1=[13,18,11,16,18,14]
>>> print(L1.clear())
[]
10) count()
This function is used to count and return number of times a value exists in a list. If
the given value is not in the list, it returns zero.
>>> L1=[13,18,11,16,18,14]
>>> L1.count(18) #18 appears twice in list L1.
2
11) reverse()
It is used to reverse the items of the list.
>>> L1=[13,18,11,16,18,14]
>>> L1.reverse()
>>> print(L1)
[14, 18, 16, 11, 18, 13]
12) sort()
This function arranges the items of the list in increasing order. We can arrange
elements in decreasing order by using function as sort (reverse=True).
>>> L1=[13,18,11,16,18,14]
>>> L1.sort()
>>> print(L1)
[11, 13, 14, 16, 18, 18]
>>> L1=[13,18,11,16,18,14]
>>> L1.sort(reverse=True)
>>> print(L1)
[18, 18, 16, 14, 13, 11]
Q.3 Write the Python statement to insert an element 200 at the third position, in the
list L1.
Ans: L1.insert(2,200)
Q.4 Identify the output of the following Python statements.
x = [[10.0, 11.0, 12.0],[13.0, 14.0, 15.0]]
y = x[1][2]
print(y)
Ans: 15.0
Q,6 A list named studentAge stores age of students of a class. Write the Python
command to import the required module and (using built-in function) to display the
most common age value from the given list.
Ans:
import statistics
print( statistics.mode(studentAge) )
Ans:
4*L
33*4
21*S
10*6