☞ 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 :L = [ 10, 20, 30, 40, ("inspire", "web", "soft"), [222,111] ] print(len(L))
☞ count() : It is used to count the occurrences of an element in the list.
Syntax :L = [ 10, 20, 30, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 ] print(L.count(10))
☞ 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> )
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))
0 3 3 3
L = [ 10, 20, 30, 10, 40, ("inspire", "web", "soft"), [10,20], 55, 10, 10 ] print(L.index(10, 1, 3))
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
L1 = [45, 67, 23, 17, 34] L1.sort() print(L1)
[17, 23, 34, 45, 67]
L1 = [45, 67, 23, 17, 34] L1.sort(reverse=True) print(L1)
[67, 45, 34, 23, 17]
L1 = [45, 67, (23, 17), 34] L1.sort(reverse=True) print(L1)
L1.sort(reverse=True) TypeError: '<' not supported between instances of 'list' and 'int'
L1 = ['X', 'A', 'C', 'c', 'a', 'x', 'Ab', 'Ad', 'Acd'] L1.sort() print(L1)
['A', 'Ab', 'Acd', 'Ad', 'C', 'X', 'a', 'c', 'x']
☞ reverse() : It is used to reverse the elements of the list.
Syntax :<list>.reverse()
L1 = [45, 67, 17, 23, 34] L1.reverse() print(L1)
[34, 23, 17, 67, 45]
L1 = [45, 67, (17, 23), "inspire", 34] L1.reverse() print(L1)
[34, 'inspire', (17, 23), 67, 45]
☞ append() : It is used to insert an element at the end of the list.
Syntax :<list>.append(<element>)
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)
[] [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>)
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)
['inspire'] ['inspire', 'soft'] ['inspire', 'web', 'soft']
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)
[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]
☞ 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()
L = [ 10, 20, 30, 40] print(L) L.clear() print(L)
[10, 20, 30, 40] []
☞ remove() : It is used to remove/delete an element from the list.
Syntax :<list>.remove(<element>)
L = [ 10, 20, 30, 40, 20, 60, 40, 20] print(L) L.remove(20) print(L)
[10, 20, 30, 40, 20, 60, 40, 20] [10, 30, 40, 20, 60, 40, 20]
L = [ 10, 20, 30, 40, 20, 60, 40, 20] L.remove(200) print(L)
L.remove(200) 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
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)
[10, 20, 30, 40, 20, 60, 40] [10, 20, 30, 20, 60, 40]
L = [ 10, 20, 30, 40, 20, 60, 40, 20] L.pop(31) print(L)
L.pop(31) 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>)
L1 = [ 10, 20 ] L2 = [30, 40, 50] print(L1) print(L2) L2.extend(L1) print(L1) print(L2)
[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)
L1=[20, 50, 10, 40, 30] a=sorted(L1) print(a)
[10, 20, 30, 40, 50]
L1=[20, 50, 10, 40, 30] a=sorted(L1, reverse=True) print(a)
[50, 40, 30, 20, 10]
☞ min() : It returns the element with minimum value from the list.
Syntax :min( <list>)
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))
10 A A AB 10.0
☞ max() : It returns the element with maximum value from the list.
Syntax :max( <list>)
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))
147 Z z za 147.5
☞ sum() : It returns the sum of elements of the list.
Syntax :sum(<list>)
L = [ 10, 20, 65, 55.5, 147, 30] print(sum(L))
327.5