Remove a tuple from a list of tuples in Python

avatar

Borislav Hadzhiev

Last updated: Jun 30, 2022

banner

Photo from Unsplash

Remove a tuple from a list of tuples in Python #

Use the del statement to remove a tuple from a list of tuples, e.g. del list_of_tuples[0]. The del statement can be used to remove a tuple from a list by its index, and can also be used to remove slices from a list of tuples.

main.py
list_of_tuples = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] print(list_of_tuples.index((1, 'one'))) # 👉️ 0 # ✅ remove tuple from list by index del list_of_tuples[0] # 👇️ [(2, 'two'), (3, 'three'), (4, 'four')] print(list_of_tuples) # --------------------------------------------- # ✅ remove tuple from list by value list_of_tuples.remove((4, 'four')) # 👇️ [(2, 'two'), (3, 'three')] print(list_of_tuples)

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

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
list_of_tuples = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] del list_of_tuples[0:2] # 👇️ [(3, 'three'), (4, 'four')] print(list_of_tuples)

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

Alternatively, you can use the remove() method.

Use the list.remove() method to remove a tuple from a list, e.g. list_of_tuples.remove((4, 'four')). The list.remove() method will remove the first tuple from the list whose value is equal to the passed in argument.

main.py
list_of_tuples = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] list_of_tuples.remove((4, 'four')) # 👇️ [(1, 'one'), (2, 'two'), (3, 'three')] print(list_of_tuples)

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The method raises a ValueError if there is no such item.

The remove() method mutates the original list and returns None.

You can also use a list comprehension to remove one or more tuples from a list.

To remove one or more tuples from a list:

  1. Use a list comprehension to iterate over the list.
  2. Return a condition from the list comprehension.
  3. The new list will only contain the tuples for which the condition is met.
main.py
list_of_tuples = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] new_list = [tup for tup in list_of_tuples if tup[0] != 3] # 👇️ [(1, 'one'), (2, 'two'), (4, 'four')] print(new_list)

We used a list comprehension to filter a list of tuples.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we check if the first item (index 0) of the current tuple is not equal to 3 and return the result.

The new list only contains the tuples for which the condition returned a truthy value.

You can also use the list.pop() method to remove a tuple from a list.

main.py
list_of_tuples = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] list_of_tuples.pop(0) # 👇️ [(2, 'two'), (3, 'three'), (4, 'four')] print(list_of_tuples) list_of_tuples.pop(list_of_tuples.index((3, 'three'))) # 👇️ [(2, 'two'), (4, 'four')] print(list_of_tuples)

The list.pop method removes the item at the given position in the list and returns it.

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

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

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.