Python List Methods

Python List Methods

Now, we are going to learn about some of the different methods, related to the list, with which, we can do a bunch of different things, like sorting the list, or appending some element to the list, clearing the list, removing some element from the list, making a copy of the list, and much much more. First of all, have a look at the table below, in which, we have the name of the method, with some description, and an example.

Python List Methods

MethodDescriptionExample
sort()This method sorts the list.(we will see this method in details). Sorting may have different criteria.list1 =[5,6,4,1,3]
list1.sort()
print(list1)

output –
[1,3,4,5,6]
append()
This method is used to add an element to the end of the list (the appended element will be the last element then). Note that you can append anything into the list, and it will be added as the last element of the list.list1 =[5,6,4,1,3]
list1.append(‘GyaniPandit’)
print(list1)

output-
[5,6,4,1,3,’GyaniPandit’]
insert()
This method is used to add some element at the specified index.
Note that the first argument in the insert method specifies the position in the list and the 2nd argument specifies the value that is to be inserted to the specified position.
list1 =[5,6,4,1,3]
list1.insert(2,90)
print(list1)

output-
[5, 6, 90, 4, 1, 3]
clear()This method simply clears the list (removes all the elements)list1 = [1, 2, 3, 4.8 ]
list1.clear()
print(list1)

output →
[]
note that is is again an empty list.
copy()
This creates a copy of list.list1 = [1, 2, 3, 4.8 ]
list2 = list1.copy()
print(list2)

output →
[1, 2, 3, 4.8 ]
pop()
This deletes the last element from the list if not specified with any argument. But if you give index of an element as an argument, it can remove that element from the list.list1 = [1, 2, 3, 4.8 ]
list1.pop()
print(list1)

output → [1, 2, 3 ]
we can also specify the pop method to remove some other value.
list1 = [1, 2, 3, 4.8 ]
list1.pop(1)
print(list1)

output → [1, 3, 4.8 ]
in this way, the element at index 1(i.e, 2) was removed using pop.
count()
This method returns the number of times the specified element is present in the list.
You have to give an argument.
list1 = [1, 2, 2, 2, 3, 4, 5 ]
c = list1.count(2)
print(c)

output →
3
this is because 2 has occurred 3 times in the list list1.
reverse()
This method reverses the list.list1 = [3, 6.8, ‘GyaniPandit’, True]
list1.reverse()
print(list1)

output →
[True, ‘GyaniPandit’, 6.8, 3]
As you can see that the list is reversed now.
index()
This method returns the index of first occurrence of the specified element.list1 = [1, 2, 2, 2, 3, 4, 5 ]
i = list1.index(2)
print(i)
remove()This method removes the element with the specified value. (If there are multiple occurrence of that element in the list then the first occurring element is deleted)list1 = [1, 2, 2, 2, 3, 4, 5 ]
list1.remove(2)
print(list1)

output →
[1, 2, 2, 3, 4, 5]
so, this element has multiple occurrences in the list. So, the first occurrence was deleted
extend(iterable)With this method, we can extend the list, by appending all the items from the iterable.List1 = [1, 2, 3]
list2 = [4, 5, 6]
list1.extend(list2)
print(list1)

output →
[1, 2, 3, 4, 5, 6]

Now, since we have gone through some methods related to list in brief, Let’s also implement them one by one, so that we can understand them in a better way.