Remove An Item From A List In Python Using Clear, Pop, Remove And Del

In this notebook, I will go over different ways of removing items from Python list.

Let us create a list of numbers.

In [1]:
list_of_numbers = [i for i in range(20)]
In [2]:
list_of_numbers
Out[2]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Clear List Using list.clear()

Python list.clear() will remove all the elements from a list.

In [3]:
list_of_numbers.clear()
In [4]:
list_of_numbers
Out[4]:
[]

Remove element from list using list.pop()

In [5]:
list_of_numbers = [i for i in range(20)]

Python list.pop() method removes the element using index. If there is no index specified, it removes the last element from the list.

In [6]:
list_of_numbers.pop()
Out[6]:
19

Above command returns the index of the element which is removed from the list. Above command will remove the 20th element from the list.

In [7]:
list_of_numbers
Out[7]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

Let us remove the element from the list by index.

In [8]:
list_of_numbers.pop(7)
Out[8]:
7

If we provide an index which is not in the list, Python list will throw an error.

In [9]:
list_of_numbers.pop(20)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-56ea35fa0f70> in <module>
----> 1 list_of_numbers.pop(20)

IndexError: pop index out of range
In [15]:
list_of_numbers
Out[15]:
[0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]

Remove element from list using list.remove() method.

We can also remove an element from list by value.

For example let us remove the element 16 from the list.

In [16]:
list_of_numbers.remove(16)
In [17]:
list_of_numbers
Out[17]:
[0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18]

Remove element from list using del

Using del, we can remove the element by index as we can do using pop().

In [18]:
del list_of_numbers[0]
In [19]:
list_of_numbers
Out[19]:
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17, 18]

To remove the last element from the list, specify -1 as index.

In [20]:
del list_of_numbers[-1]
In [21]:
list_of_numbers
Out[21]:
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17]

Using del, we can also delete a range of numbers. In the example below, we have specified the range as 0:1, which means starting from zeroth index delete everything until index 1 (not including value at index 1).

In [22]:
del list_of_numbers[0:1]
In [23]:
list_of_numbers
Out[23]:
[2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17]

In below example, values at index 0 and 1 will be deleted.

In [24]:
del list_of_numbers[0:2]
In [25]:
list_of_numbers
Out[25]:
[4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 17]

We can also use del to remove items at fixed number of intervals.

In below example, we are deleting every 2nd number from index 2 to 8.

In [26]:
del list_of_numbers[2:8:2]
In [27]:
list_of_numbers
Out[27]:
[4, 5, 8, 10, 12, 13, 14, 15, 17]

Another example: We are deleting every 3rd number starting from index 0 to 8.

In [28]:
del list_of_numbers[0:8:3]
In [29]:
list_of_numbers
Out[29]:
[5, 8, 12, 13, 15, 17]

Remove elements using Python List Comprehension

If we want to remove elements using a condition, Python list comprehensions are very useful.

Let us remove all the elements which are multiple of 2.

In [30]:
print([i for i in list_of_numbers if i % 2 != 0])
[5, 13, 15, 17]

Remove element from list of strings

All the above list removal methods also work even if list contains a data type which is not numbers.

In [31]:
list_of_strings = ['John', 'Stacy', 'Renee', 'Jeff']
In [32]:
list_of_strings.pop()
Out[32]:
'Jeff'
In [33]:
list_of_strings.remove('Stacy')
In [34]:
list_of_strings
Out[34]:
['John', 'Renee']