Python OOPs


Python SQLite


Examples


Others


Python List Methods


Python list is used to store multiple values in order sequence which is Changeable and allow Duplicates. It can contain heterogeneous values. We can create a list using square bracket.

Method Description
count() Used to count the given element in the list.
index() Used to return the index of the element.
insert() Used to insert the first element at the given index.
remove() Used to remove the first element of the given value.
pop() Used to remove the element at the given index.
append() Used to add the element to end of the list.
extend() Used to add the elements(any iterable) to end of the list.
copy() Used to return copy of the list.
clear() Used to Remove all the elements from the list.
sort() Used to sort the list.
reverse() Used to reverse the list.

count() Method
a=[2,3,5,1,3,4,5,4]
print(a.count(4))
Output
2

index() Method
a=[2,3,5,1,3,4,5,4]
print(a.index(4))
Output
5

insert() Method
a=[2,3,5,1,3,4,5,4]
a.insert(2,50)
print(a)
Output
[2, 3, 50, 5, 1, 3, 4, 5, 4]

remove() Method
a=[2,3,5,1,3,4,5,4]
a.remove(5)
print(a)
Output
[2, 3, 1, 3, 4, 5, 4]

pop() Method
a=[2,3,5,1,3,4,5,4]
a.pop(1)
print(a)
Output
[2, 5, 1, 3, 4, 5, 4]

append() Method
a=[6,2,5]
a.append(10)
print(a)
Output
[6, 2, 5, 10]

extend() Method
a=[6,2,5]
b=[10,5,96]
a.extend(b)
print(a)
Output
[6, 2, 5, 10, 5, 96]

copy() Method
a=[6,2,5]
b=a.copy()
print(b)
Output
[6, 2, 5]

clear() Method
a=[6,2,5]
a.clear()
print(a)
Output
[]

sort() Method
a=[6,2,1,4,5,3]
a.sort()
print("Ascending Sort")
print(a)

print("Descending  Sort")
b=[6,2,1,4,5,3]
b.sort(reverse=True)
print(b)
Output
Ascending Sort
[1, 2, 3, 4, 5, 6]

Descending Sort
[6, 5, 4, 3, 2, 1]

reverse() Method
a=[6,2,1,4,5,3]
a.reverse()
print(a)
Output
[3, 5, 4, 1, 2, 6]