How to Remove an element from a Tuple in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Remove an element from a tuple in Python
  2. Remove an element from a tuple using slicing
  3. Remove an element from a tuple using conversion to list
  4. Remove an element from a tuple using filter()
  5. Remove an element from a tuple using a for loop

# 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 = ('bobby', 'hadz', 'com', 'abc') new_tuple = tuple( item for item in my_tuple if item != 'bobby' ) # ๐Ÿ‘‡๏ธ ('hadz', 'com', 'abc') print(new_tuple)

remove element from tuple

The code for this article is available on GitHub
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 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 bobby and return the result.

main.py
my_tuple = ('bobby', 'hadz', 'com', 'abc') new_tuple = tuple( item for item in my_tuple if item != 'bobby' ) # ๐Ÿ‘‡๏ธ ('hadz', 'com', 'abc') print(new_tuple)
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 based on a value or a condition.

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

# Remove an element from a tuple using slicing

This is a three-step process:

  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 = ('bobby', 'hadz', 'com', 'abc') idx = my_tuple.index('hadz') new_tuple = my_tuple[:idx] + my_tuple[idx+1:] # ๐Ÿ‘‡๏ธ ('bobby', 'com', 'abc') print(new_tuple)

remove element from tuple using slicing

The code for this article is available on GitHub

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

main.py
my_tuple = ('bobby', 'hadz', 'com', 'abc') idx = my_tuple.index('hadz') # ๐Ÿ‘‡๏ธ ('bobby',) print(my_tuple[:idx]) # ๐Ÿ‘ˆ๏ธ before # ๐Ÿ‘‡๏ธ ('com', 'abc') print(my_tuple[idx+1:]) # ๐Ÿ‘ˆ๏ธ after new_tuple = my_tuple[:idx] + my_tuple[idx+1:] # ๐Ÿ‘‡๏ธ ('bobby', 'com', 'abc') print(new_tuple)

The syntax for tuple slicing is tuple[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.

# Remove an element from a tuple using conversion to list

This is a three-step process:

  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 = ('bobby', 'hadz', 'com', 'abc') my_list = list(my_tuple) my_list.remove('hadz') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'abc'] new_tuple = tuple(my_list) print(new_tuple) # ๐Ÿ‘‰๏ธ ('bobby', 'com', 'abc')

remove element from tuple using conversion to list

The code for this article is available on GitHub

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 = ('bobby', 'hadz', 'com', 'abc') my_list = list(my_tuple) try: my_list.remove('hadz') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'com', 'abc'] except ValueError: print('Item not in list') new_tuple = tuple(my_list) print(new_tuple) # ๐Ÿ‘‰๏ธ ('bobby', 'com', 'abc')

using try except to handle item not in list

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

# Remove an element from a tuple using filter()

You can also use the filter() function to remove an element from a tuple.

main.py
my_tuple = (1, 2, 3, 4, 5, 6) new_tuple = tuple(filter(lambda x: x > 3, my_tuple)) print(new_tuple) # ๐Ÿ‘‰๏ธ (4, 5, 6)

remove element from tuple using filter

The code for this article is available on GitHub

The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

The lambda function we passed to filter() gets called with each element of the tuple.

The function checks if the value is greater than 3 and returns the result.

The filter object only contains elements from the tuple that meet the condition.

The last step is to use the tuple constructor to convert the filter object to a list.

# Remove an element from a tuple using a for loop

You can also use a for loop to remove an element from a tuple.

main.py
my_tuple = (1, 2, 3, 4, 5, 6) my_list = [] for item in my_tuple: if item > 3: my_list.append(item) new_tuple = tuple(my_list) print(new_tuple) # ๐Ÿ‘‰๏ธ (4, 5, 6)

remove element from tuple using for loop

The code for this article is available on GitHub

We used a for loop to iterate over the tuple.

On each iteration, we check if a certain condition is met.

If the condition is met, we append the item to a new list.

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

I've also written an article on how to insert/append an element into a tuple.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev