Remove an element from a list by index in Python

avatar

Borislav Hadzhiev

Last updated: Jun 30, 2022

banner

Photo from Unsplash

Remove an element from a list by index in Python #

Use the del statement to remove an element from a list by index, e.g. del my_list[2]. The del statement can be used to remove an element from a list by its index, and can also be used to remove slices from a list.

main.py
my_list = ['zero', 'one', 'two', 'three'] print(my_list.index('two')) # 👉️ 2 # 👇️ remove list element by index del my_list[2] print(my_list) # 👉️ ['zero', 'one', 'three'] # 👇️ remove slice of list elements del my_list[0:2] print(my_list) # 👉️ ['three']

The first example uses the del statement to remove an element from a list.

Python indexes are zero-based. The first item in a list has an index of 0, the second an index of 1, and the last an index of -1.

The del statement is used to remove an item from a list by its index instead of its value.

You can also use the del statement to remove a slice from the list.

main.py
my_list = ['zero', 'one', 'two', 'three'] print(my_list[0:2]) # 👉️ ['zero', 'one'] del my_list[0:2] print(my_list) # 👉️ ['two', 'three']

The syntax for list slicing is my_list[start:stop:step] where the start index is inclusive and the stop index is exclusive.

The example removes the list items at index 0 and 1. Note that the element at index 2 is not removed because the stop index is exclusive.

Alternatively, you can use the list.pop() method.

Use the list.pop() method to remove an element from a list by index, e.g. my_list.pop(0). The list.pop method removes the item at the given position in the list and returns it.

main.py
my_list = ['zero', 'one', 'two', 'three'] my_list.pop(0) # 👇️ ['one', 'two', 'three'] print(my_list)

If no index is specified, the pop() method removes and returns the last item in the list.

main.py
my_list = ['zero', 'one', 'two', 'three'] my_list.pop() # 👇️ ['zero', 'one', 'two'] print(my_list)

The main difference between the pop method and the del statement is that list.pop() returns the removed value.

main.py
my_list = ['zero', 'one', 'two', 'three'] print(my_list.pop(0)) # 👉️ zero

Alternatively, you can use list slicing.

This is especially useful when you don't want to mutate the original list, but instead want to create a new list that doesn't contain the item at a specific index.
main.py
my_list = ['zero', 'one', 'two', 'three'] idx = 2 new_list = my_list[0:idx] + my_list[idx+1:] print(new_list) # 👉️ ['zero', 'one', 'three']

We used the addition (+) operator to combine two list slices.

main.py
# 👇️ ['zero', 'one', 'two'] print(['zero', 'one'] + ['two'])

The first slice starts at index 0 and goes up to but not including the item at the specified index.

main.py
my_list = ['zero', 'one', 'two', 'three'] idx = 2 print(my_list[0:idx]) # 👉️ ['zero', 'one'] print(my_list[idx+1:]) # 👉️ ['three'] new_list = my_list[0:idx] + my_list[idx+1:]

The second slice starts at the specified index + 1 because we don't want to include the item at that index in the new list.

Start indexes are inclusive and stop indexes are exclusive in list slicing.

This approach doesn't remove an element from a list, instead it creates a new list without the element at the specified index.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.