How to remove items from a Python List


Today’s tutorial is in response to an email from a reader who asked me how to remove items from a Python list.

I’ve decided to write an article listing the different ways to do so. Here we go.

There are five ways to do it:

  1. Using the remove() method
  2. Using the del keyword
  3. Using the pop() method
  4. Using slice notation
  5. Using list comprehension

Using the remove() method

If you want to remove an item from the list given its value, you can use the remove() method. This method takes in the value to remove as an argument and searches for the value in the list. It then removes the first matching element.

If the value passed in to the remove() method does not exist, a ValueError exception is thrown (see Example 2).

Example 1

list1 = [7, 11, 21, 4, 5, 11, 5]

#remove the first 11 
list1.remove(11)

print(list1)


Output

[7, 21, 4, 5, 11, 5]

Example 2

list2 = [14, 121, 31, 15, 21, 9]

#value does not exist 
list2.remove(30)


Output

Traceback (most recent call last):
File "…", line 4, in
list2.remove(30)
ValueError: list.remove(x): x not in list

Using the del keyword

The remove() method is useful for removing an item from a list using its value.

If you want to remove an item from the list based on its index instead, you can use the del keyword.

If the index is out of range, an IndexError will be raised.

If you do not state the index, the entire list will be deleted.

Example 3

list3 = [1, 4, 7, 1, 2, 0]

#delete the fifth item
del list3[4]

print(list3)


Output

[1, 4, 7, 1, 0]

Example 4

list4 = [5, 1, 8, 9, 8]

#index 10 is out of range
del list4[10]


Output

Traceback (most recent call last):
File "…", line 4, in
del list4[10]
IndexError: list assignment index out of range

Example 5

list5 = [45, 12, 8, 3, 9, 4]

#delete the entire list
del list5

print(list5) #This will generate an error as list5 has been deleted.


Output

Traceback (most recent call last):
File "…", line 6, in
print(list5)
NameError: name 'list5' is not defined

Using the pop() method

Similar to the del keyword, the pop() method can also be used to remove an item from the list based on its index.

This method takes the index of the item as an argument and removes the item at that index. It also returns the item deleted.

If the index is not given, it deletes the last element.

The method raises an IndexError if the index is out of range.

Example 6

list6 = ['cat', 'dog', 'hamster', 'rabbit', 'turtle']

#remove the last item (the default when no index is given)
itemRemoved = list6.pop()

print(itemRemoved)
print(list6)


Output

turtle
['cat', 'dog', 'hamster', 'rabbit']

Example 7

list7 = ['poodle', 'corgi', 'jack russell']

#delete the second item
list7.pop(1)

print(list7)


Output

['poodle', 'jack russell']

Example 8

list8 = ['persian', 'maine coon', 'munchkin']

#index is out of range
list8.pop(5)


Output

Traceback (most recent call last):
File "…", line 4, in
list8.pop(5)
IndexError: pop index out of range

Using slice notation

Technically speaking, the slice notation does not remove items from a list. Instead, we can use it to create a new list with the elements that we want. We can then assign this new list back to the original list.

Suppose list9 = [4, 23, 6, 3, 8, 2] and you only want the first 3 items from the list. The example below shows how you can get a new list using the slice notation. This new list is assigned back to list9.

Example 9

list9 = [4, 23, 6, 3, 8, 2]
list9 = list9[0:3]

print(list9)


Output

[4, 23, 6]

Using list comprehension

List comprehension is another way for us to create a new list with elements that we want.

It provides us with a concise way of creating these lists using for loops and if conditions. Let’s look at a brief introduction first.

Suppose original_list = [1, 2, 5, 3, 1, 7] and you want to do the following:

If the item is greater than 3, multiply it by 10 and add it to a new list called new_list. Here’s how you can do it using the ‘traditional’ way:

Output

[1, 2, 5, 3, 1, 7]
[50, 70]

However, another way to do it is to use list comprehension as shown below:

This is exactly the same as the for loop above, but presented in a more concise manner.

Let’s look at how we can use list comprehension to create a new list with items removed from an original list. We can then assign this new list back to the original list.

Suppose list10 = [1, 21, 20, 5, 6, 9] and we want to remove all items smaller than 7 from the list, here’s how we can do it:

Example 10

list10 = [1, 21, 20, 5, 6, 9]

list10 = [i for i in list10 if i >= 7]

print(list10)


Output

[21, 20, 9]

Written by Jamie | Last Updated November 11, 2019

Recent Posts