All List Methods in Python

Praveen NG
4 min readFeb 19, 2024

--

In this story, I am going to describe every methods for Python lists. There are 11 list methods. I am describing them alphabetically.

  1. append

Append is used to add or append a new element to a list. Only one element can be appended to a list. Append does not return any values, and appending is done in-place.

mylist = [1, 2]
mylist.append(3)# mylist will be [1, 2, 3] after this operation

If you append another list, the list itself will be added as an element.

mylist = [1, 2]
mylist.append([3, 4]) # my list will be [1, 2, [3, 4]) after this operation

2. clear

The method clear is used to clear or empty a list.

mylist = [1, 2]
mylist.clear() # mylist will be an empty list [] after this operation

3. copy

This method is used to copy a list.

mylist1 = [10, 20]
mylist2 = mylist1.copy()

Copy method is especially useful if you need to modify the new list without modifying the original.

mylist1 = [10, 20]
mylist2 = mylist1.copy()
mylist2.append(30)
#mylist1 will be [10, 20] and mylist2 will be [10, 20, 30]
#after the last operation

In the above scenario, if you don’t use copy method, you may inadvertently modify the first list as well.

mylist1 = [10, 20]
mylist2 = mylist1 #not using copy() method!
mylist2.append(30)
#both mylist1 and mylist2 will be both [10, 20, 30] in this case

This happens because when you state mylist2 = mylist1, the python interpreter adds to mylist2 a reference to mylist1 object. It is similar to how pointers work in C.

4. Count

The count method returns the number of times an item occurs in a list.

mylist1 = [10, 20, 30, 10]
mylist1.count(10) # returns 2
mylist1.count(20) # returns 1

If one of elements itself is a list, it’s sub-items are not counted.

mylist1 = [10, 20, 30, [10, 20]]
mylist1.count(10)
#returns 1 because the fourth item is a list [10, 20],
# so it is not counted

mylist1.count([10, 20]) # returns 1 because [10, 20] is the fourth item

5. extend

Extend is similar to append, but it can add multiple elements to a list.

mylist1 = ['a', 'b']
mylist1.extend(['c', 'd']) # mylist1 will be ['a', 'b', 'c', 'd']

#if you use append instead
mylist1 = ['a', 'b']
mylist1.append(['c', 'd']) # mylist1 will be ['a', 'b', ['c', 'd']]

Notice how append and extend differs in the above example.

6. index

The index method returns the index of the first occurance of an item.

mylist1 = [10, 20, 30, 40]
mylist1.index(20) # returns 1 because 20 is the second item
# (so index 1 since the first item has an index 0)

mylist1 = [10, 20, 30, 40, 10] # 10 is entered twice
mylist1.index(10) # returns 0 because it first occurred
# at first position (index 0)

7. insert

This methods takes two parameters. The first one is an index describing where do you want to insert a new item. The second parameter is the item you want to insert.

fruits = ['apple', 'orange', 'banana'] 
fruits.insert(1, 'grape')
#fruits will be ['apple', 'grape', 'orange', 'banana'] after the last operation

8. pop

This method optionally takes in an integer as parameter. The parameter is an index and the method removes the item at the specified index.

fruits = ['apple', 'grape', 'orange', 'banana']
fruits.pop(2)
#returns 'orange' (item at index 2). 'orange is removed from the list
# the fruits list will be ['apple', 'grape', 'banana'] after the
# last operation

If no parameter is given, the default value is -1, so the last item is removed from the list and returned.

fruits = ['apple', 'grape', 'orange', 'banana']
fruits.pop()
# returns 'banana'
# fruits will be ['apple', 'grape', 'orange']

9. remove

This method removes the first occurrence of an item.

letters = ['a', 'b', 'c', 'd', 'a'] # 'a' is repeated 
letters.remove('a')
#letters will be ['b', 'c', 'd', 'a'] # the last element 'a' is not removed

10. reverse

This reverse method reverses the order of items in a list.

letters = ['a', 'b', 'c', 'd']
letters.reverse()
# letters will be ['d', 'c', 'b', 'a']

Notice that reverse is an in-place operation — the list is reversed but nothing is returned by the method.

11. sort

As the method’s name suggests, it sorts a list.

letters = ['a', 'c', 'd', 'b']
letters.sort()
# letters will be ['a', 'b', 'c', 'd']

Just like reverse, sort also does not return any value and it sorts the list in-place.

Sort may return an error if the items are not comparable by operators such as > or <

mylist1 = ['a', 'A', 10, 1]
mylist1.sort()
TypeError: '<' not supported between instances of 'int' and 'str'
# it is not possible to check if 'a' < 10

mylist1 = [10, 1, [8, 10]]
TypeError: '<' not supported between instances of 'list' and 'int'
#the third item itself is a list

So, those are the 11 Python list methods. I hope that this post would be useful for some of the readers. If you find it useful consider giving it a clap and following me.

--

--

Praveen NG
Praveen NG

Written by Praveen NG

A data science professional with a research background.

No responses yet