Remove an element from a tuple in Python

avatar

Borislav Hadzhiev

Last updated: Jun 26, 2022

banner

Photo from Unsplash

Remove an element from a tuple in Python #

To remove an element from a tuple:

  1. Use a generator expression to iterate over the tuple.
  2. On each iteration, check if the element satisfies a condition.
  3. Use the tuple() class to convert the result to a tuple.
main.py
my_tuple = ('one', 'two', 'three', 'four') # ✅ remove one or more items from a tuple # 👇️ using a generator expression new_tuple_1 = tuple( item for item in my_tuple if item != 'two' ) # 👇️ ('one', 'three', 'four') print(new_tuple_1) # ------------------------------------------ # ✅ remove specific item from a tuple idx = my_tuple.index('two') new_tuple_2 = my_tuple[:idx] + my_tuple[idx+1:] # 👇️ ('one', 'three', 'four') print(new_tuple_2)
Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

Since tuples cannot be changed, the only way to remove an element from a tuple is to create a new tuple that doesn't contain the element.

The first example uses a generator expression to iterate over the tuple.

On each iteration, we check if the tuple item is not equal to the string two and return the result.

main.py
my_tuple = ('one', 'two', 'three', 'four') new_tuple_1 = tuple(item for item in my_tuple if item != 'two') # 👇️ ('one', 'three', 'four') print(new_tuple_1)
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

Make sure to pass the generator object to the tuple() class to convert it to a tuple.

This approach is useful when you need to remove one or more elements from a tuple.

Alternatively, you can get the index of the item you are trying to remove and create a new tuple without the item.

To remove an item from a tuple:

  1. Use the index() method to get the index of the item to be removed.
  2. Use tuple slicing to get slices of the items before and after the item.
  3. Use the addition (+) operator to combine the two slices.
main.py
my_tuple = ('one', 'two', 'three', 'four') idx = my_tuple.index('two') new_tuple_2 = my_tuple[:idx] + my_tuple[idx+1:] # 👇️ ('one', 'three', 'four') print(new_tuple_2)

The first slice [:idx] selects the tuple items before the one we want to remove, and the second [idx+1:] selects the items after the one we want to remove.

main.py
my_tuple = ('one', 'two', 'three', 'four') idx = my_tuple.index('two') # 👇️ ('one',) print(my_tuple[:idx]) # 👈️ before # 👇️ ('three', 'four') print(my_tuple[idx+1:]) # 👈️ after new_tuple_2 = my_tuple[:idx] + my_tuple[idx+1:] # 👇️ ('one', 'three', 'four') print(new_tuple_2)

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

The start index is inclusive, so we had to add 1 to the index of the element in the second slice.

The last step is to use the addition (+) operator to create a new tuple by combining the other two.

To remove an element from a tuple:

  1. Convert the tuple to a list (lists are mutable).
  2. Use the list.remove() method to remove the item from the list.
  3. Convert the list back to a tuple.
main.py
my_tuple = ('one', 'two', 'three', 'four') my_list = list(my_tuple) my_list.remove('two') print(my_list) # 👉️ ['one', 'three', 'four'] new_tuple = tuple(my_list) print(new_tuple) # 👉️ ('one', 'three', 'four')

Note that this approach is not very efficient when working with large tuples.

We used the list() class to convert the tuple to a list.

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.

If you need to handle a scenario where the item is not found in the list, use a try/except statement.

main.py
my_tuple = ('one', 'two', 'three', 'four') my_list = list(my_tuple) try: my_list.remove('two') print(my_list) # 👉️ ['one', 'three', 'four'] except ValueError: print('Item not in list') new_tuple = tuple(my_list) print(new_tuple) # 👉️ ('one', 'three', 'four')

The last step is to use the tuple() class to convert the list back to a tuple.

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.