Remove element from list python

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)]

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.

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.

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

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.

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

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

IndexError: pop index out of range

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)

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().

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.

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).

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.

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]

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]

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])

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 [33]:

list_of_strings.remove('Stacy')

How do I remove one element from a list?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

What does remove () do in Python?

The remove() method removes the first occurrence of the element with the specified value.

How do I remove a specific index from a list?

You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index. Therefore, a[2] contains 3 and pop() removes and returns the same as output. You can also use negative index values.