Python Remove Element from List – pop() and remove()

In this post, I will show you how to remove an element from a list in python. The main function to use is the built-in remove() functionality, del() and pop().

List Remove() Function in Python

The remove() function is as easy as it sounds:

list.remove(item)

The syntax, as you can see, is very easy: we just need to pass the item we want to remove from the list.

The parameter of the remove() function is only the item value in the list:

  • The remove() only takes one item as parameter, the name of the item, and removes it from the list
  • The function will return an error of “ValueError: list.remove(x): x not in list ” if the item is not found in the list.

The remove() function, similarly to other functionalities in python, does not give back any value (it is a method, rather than an actual function).

So, let’s get some code with examples, shall we?

Remove Element from List in Python, Example 1

The easiest example can be a list of strings such as:

# Cities list
cities = ['new york', 'london', 'barcelona', 'marrakech']

Now, if we want to remove ‘london’ from the list (because of the bad weather, why not?). We can pass the item to the remove function, such as:

# Remove 'london' from the list
cities.remove('london')
# If we print cities now:
print(cities)
# Output: ['new york', 'barcelona', 'marrakech']

Very easy, right?

What would happen if the value of ‘london’ was repeated in the list?

Remove Duplicate Element from list in Python, Example 2

Let’s go back to our same example as before:

# Cities list
cities = ['new york', 'london', 'barcelona', 'marrakech', 'london']

As you can see, ‘london’ is repeated twice in this list, what would happen if we use the remove() function:

# Remove 'london' from the list
cities.remove('london')
# If we print cities now:
print(cities)
# Output: ['new york', 'barcelona', 'marrakech', 'london']

It will only remove the first ‘london’ item found in the list. Very handy, right? Python is very handy in this situations!

What if we pass a value that does not exist in the list?

Remove Element that doesn’t exist from List in Python, Example 3

In this case, Python will break and will display to us the exception mentioned in the first paragraphs:

# Cities list
cities = ['new york', 'london', 'barcelona', 'marrakech', 'london']
# Remove 'tokyo' from the list
cities.remove('tokyo')

If we run the above script, an exception will be raised showing exactly why we can’t remove this element:

Traceback (most recent call last):
  File ".. .. ..", line 5, in <module>
    cities.remove('tokyo')
ValueError: list.remove(x): x not in list

In this case, the x item is ‘tokyo’ which does not exist in the list defined above.

List Pop() Function in Python

The pop() function is very similar to the remove function mentioned above. The only difference in this case, is that it doesn’t take as an argument the actual item, but the position index of it.

list.pop(index)

Quite straightforward, we take a list and we remove (pop from the list) the item in index.

  • The pop() item takes only one argument, which is the index of the position of the item we want to remove. This is an optional argument, as we can call the function without passing any and it will remove the last element of the list.
  • If the index we are sending to the function is out of range, python will raise an exception: IndexError: pop index out of range

Like the previous function, pop(), does not return any value when called. Let’s go now check some examples!

Remove Element from List using pop, Example 1

We can still use the same list as before, our beloved cities:

# Cities list
cities = ['new york', 'london', 'barcelona', 'marrakech']

Now we want to remove element from list ‘marrakech’, as it is very hot in the summer! We have to know the index position of the item, in this case is 3. Remember Python starts indexing from value 0.

# Remove 'marrakech' from the list
cities.pop(3)
# If we print cities now:
print(cities)
# Output: ['new york', 'london', 'barcelona']

So the only big thing here is to know the position of the item we want to delete.

Remove Last Element from List using pop, Example 2

We can use the pop() function without passing an index. It will remove the last item of the list:

# Cities list
cities = ['new york', 'london', 'barcelona', 'marrakech']
# Remove last item from the list
cities.pop()
# If we print cities now:
print(cities)
# Output: ['new york', 'london', 'barcelona']

This is called a stack data structure, where the pop function removes the last element of the list.

Conclusion

There are many ways to remove an item from a list in python. In this post I have showed you two methods: remove() and pop(). Depending on the context you are in you will want to use one or another method.

In any case, options in Python to remove an item from a list are quite handy and easy to use!

Leave a Comment