Last updated: Apr 9, 2024
Reading timeยท5 min
To remove an element from a tuple:
tuple()
class to convert the result to a tuple.my_tuple = ('bobby', 'hadz', 'com', 'abc') new_tuple = tuple( item for item in my_tuple if item != 'bobby' ) # ๐๏ธ ('hadz', 'com', 'abc') print(new_tuple)
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.
my_tuple = ('bobby', 'hadz', 'com', 'abc') new_tuple = tuple( item for item in my_tuple if item != 'bobby' ) # ๐๏ธ ('hadz', 'com', 'abc') print(new_tuple)
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.
This is a three-step process:
index()
method to get the index of the item to be removed.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)
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.
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.
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.
This is a three-step process:
list.remove()
method to remove the item from the list.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')
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.
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')
The last step is to use the tuple()
class to convert the list back to a tuple.
You can also use the filter()
function to remove an element from a tuple.
my_tuple = (1, 2, 3, 4, 5, 6) new_tuple = tuple(filter(lambda x: x > 3, my_tuple)) print(new_tuple) # ๐๏ธ (4, 5, 6)
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.
for
loopYou can also use a for loop to remove an element from a tuple.
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)
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.
You can learn more about the related topics by checking out the following tutorials: