Some Functions of List


☞ Following are the some functions of List.

1. len()
2. count()
3. index()
4. sort()
5. reverse()
6. append()
7. insert()
8. clear()
9. remove()
10. pop()
11. extend()
12. sorted()
13. min()
14. max()
15. sum()

len() : It returns the length of the list.

Syntax :
len( <list>)
Example :
 
L = [ 10, 20, 30, 40, ("inspire", "web", "soft"), [222,111] ]
print(len(L)) 
Output
6

count() : It is used to count the occurrences of an element in the list.

Syntax :
<list>.count( <element> )
Example :
 
L = [ 10, 20, 30, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 ]
print(L.count(10)) 
Output
3

index() : It finds the first index of a given element and returns the index.

Syntax :
 
<list>.index( <element> )
<list>.index( <element>, <start_index> )
<list>.index( <element>, <start_index>, <stop_index> )
Example 1 :
 
L = [ 10, 20, 30, 10, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 ]
print(L.index(10))
print(L.index(10, 2))
print(L.index(10, 2, 9))
print(L.index(10, 2, 15))
Output
0
3
3
3
Example 2 :
 
L = [ 10, 20, 30, 10, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 ]
print(L.index(10, 1, 3)) 
Output
print(L.index(10, 1, 3))
ValueError: list.index(x): x not in list

sort() : It is used to sort the elements in ascending or descending order in the list. By default it will be sorted in ascending order.

Syntax :
<list>.sort()                       #Ascending order
<list>.sort(reverse=True)        #Descending order
Example 1 :
 
L1 = [45, 67, 23, 17, 34]
L1.sort()
print(L1)
Output
[17, 23, 34, 45, 67]
Example 2 :
 
L1 = [45, 67, 23, 17, 34]
L1.sort(reverse=True)
print(L1)
Output
[67, 45, 34, 23, 17]
Example 3 :
 
L1 = [45, 67, (23, 17), 34]
L1.sort(reverse=True)
print(L1)
Output
L1.sort(reverse=True)
TypeError: '<' not supported between instances of 'list' and 'int'
Example 4 :
 
L1 = ['X', 'A', 'C', 'c', 'a', 'x', 'Ab', 'Ad', 'Acd']
L1.sort()
print(L1)
Output
['A', 'Ab', 'Acd', 'Ad', 'C', 'X', 'a', 'c', 'x']
NOTE : If you will apply a sort() on list containing other datatype like list, tuple, dictionary, it will give you an TypeError: '<' not supported between instances of 'tuple/list/dictionary' and 'int'.

reverse() : It is used to reverse the elements of the list.

Syntax :
<list>.reverse()     
Example 1 :
 
L1 = [45, 67, 17, 23, 34]
L1.reverse()
print(L1)
Output
[34, 23, 17, 67, 45]
Example 2 :
 
L1 = [45, 67, (17, 23), "inspire",  34]
L1.reverse()
print(L1)
Output
[34, 'inspire', (17, 23), 67, 45]
NOTE : If you will write print(<list>.reverse()), then it will print None as an answer.

append() : It is used to insert an element at the end of the list.

Syntax :
<list>.append(<element>)     
Example :
 
L = [ ]
print(L)

L.append(10)
print(L)

L.append([20,30])
print(L)

L.append((40,50))
print(L)

L.append("inspire")
print(L)

L.append({'A':12})
print(L)
Output
[]
[10]
[10, [20, 30]]
[10, [20, 30], (40, 50)]
[10, [20, 30], (40, 50), 'inspire']
[10, [20, 30], (40, 50), 'inspire', {'A': 12}]

insert() : It is used to insert an element at the particular index of the list.

Syntax :
<list>.insert(<index> , <element>)     
Example 1 :
 
L = [ ]
L.insert(15,"inspire")  #it will be added to 0 index
print(L)

L.insert(12,"soft")   #it will be added at the last
print(L)

L.insert(1,"web")    #now it will be at 1 index
print(L)
Output
['inspire']
['inspire', 'soft']
['inspire', 'web', 'soft']
Example 2 :
 
L = [ 10, 20, 30, 40]

L.insert(1,'A')
print(L)

L.insert(-2,'Z')
print(L)

L.insert(-1,'X')
print(L)

L.insert(0,'B')
print(L)
Output
[10, 'A', 20, 30, 40]
[10, 'A', 20, 'Z', 30, 40]
[10, 'A', 20, 'Z', 30, 'X', 40]
['B', 10, 'A', 20, 'Z', 30, 'X', 40]
NOTE : If you will write <list>.insert(<one_argument>), then it will give TypeError : insert expected 2 arguments, got 1.

clear() : It is used to remove/delete all the elements from the list. After removing all the elements, list will become empty.

Syntax :
<list>.clear()     
Example :
 
L = [ 10, 20, 30, 40]
print(L)

L.clear()
print(L)
Output
[10, 20, 30, 40]
[]

remove() : It is used to remove/delete an element from the list.

Syntax :
<list>.remove(<element>)     
Example 1 :
 
L = [ 10, 20, 30, 40, 20, 60, 40, 20]
print(L)

L.remove(20)
print(L)
Output
[10, 20, 30, 40, 20, 60, 40, 20]
[10, 30, 40, 20, 60, 40, 20]
Example 2 :
 
L = [ 10, 20, 30, 40, 20, 60, 40, 20]
L.remove(200)
print(L)
Output
L.remove(200)
ValueError: list.remove(x): x not in list
NOTE : If you will pass an element which is not in the list, then it will give ValueError : list.remove(x) : x not in list.

pop() : It is used to remove/delete an element from the list.

Syntax :
<list>.pop()       #deletes the last element
<list>.pop(<index>)     #deletes the element at particular index
Example 1 :
 
L = [ 10, 20, 30, 40, 20, 60, 40, 20]

L.pop()    #removes last element
print(L)

L.pop(3)    #removes element of 3rd index
print(L)
Output
[10, 20, 30, 40, 20, 60, 40]
[10, 20, 30, 20, 60, 40]
Example 2 :
 
L = [ 10, 20, 30, 40, 20, 60, 40, 20]
L.pop(31)   
print(L)
Output
L.pop(31)
IndexError: pop index out of range
NOTE : If you will pass an index which is not in the list, then it will give IndexError : pop index out of range.

extend() : It is used to add elements of one list into the other and update itself.

Syntax :
<list_1>.extend(<list_2>)  
Example :
 
L1 = [ 10, 20 ]
L2 = [30, 40, 50]
print(L1)
print(L2)
L2.extend(L1)   
print(L1)
print(L2)
Output
[10, 20]
[30, 40, 50]
[10, 20]
[30, 40, 50, 10, 20]

sorted() : It is used to sort the elements of a list. It returns the list after sorting.

Syntax :
sorted( <list>)
sorted( <list>, reverse = True)
Example 1 :
 
L1=[20, 50, 10, 40, 30]
a=sorted(L1)
print(a)
Output
[10, 20, 30, 40, 50]
Example 2 :
 
L1=[20, 50, 10, 40, 30]
a=sorted(L1, reverse=True)
print(a)
Output
[50, 40, 30, 20, 10]

min() : It returns the element with minimum value from the list.

Syntax :
min( <list>)
Example :
 
L1 = [10, 20, 65, 55, 147, 30]
print(min(L1))

L2 = [ "Z", "A", "W" ]
print(min(L2))

L3 = [ "A", "a", "Z", "z" ]
print(min(L3))

L4 = ["AB", "Ab", "aB", "ZA", "zA", "za" ]
print(min(L4))

L5 = [ 10.0, 20.5, 65, 55, 147.5, 30]
print(min(L5))
Output
10
A
A
AB
10.0

max() : It returns the element with maximum value from the list.

Syntax :
max( <list>)
Example :
 
L1 = [10, 20, 65, 55, 147, 30]
print(max(L1))

L2 = [ "Z", "A", "W" ]
print(max(L2))

L3 = [ "A", "a", "Z", "z" ]
print(max(L3))

L4 = ["AB", "Ab", "aB", "ZA", "zA", "za" ]
print(max(L4))

L5 = [ 10.0, 20.5, 65, 55, 147.5, 30]
print(max(L5))
Output
147
Z
z
za
147.5
Note : max() and min() check for the characters in a string on the basis of their ASCII values. If there are two strings in a list, then the first different character in the strings is checked

sum() : It returns the sum of elements of the list.

Syntax :
sum(<list>)
Example :
 
L = [ 10, 20, 65, 55.5, 147, 30]
print(sum(L))
Output
327.5