Python – Remove first occurrence of K in Tuple

Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of first occurrence of an element in tuple. This type of problem can have application in many domains such as school programming. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple = (5, 6, 5, 4, 7, 8, 4), K = 5
Output : (6, 5, 4, 7, 8, 4)

Input : test_tuple = (5, 6, 8, 4, 7, 8, 4), K = 8
Output : (5, 6, 4, 7, 8, 4)

Method #1 : Using index() + loop + list slicing
The combination of above functions can be used to solve this problem. In this, we perform the task of extracting the first occurrence of K using index() and list slicing is used to reorder the tuple after element removal.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Remove first occurrence of K in Tuple
# Using index() + loop + list slicing
  
# initializing tuples
test_tuple = (5, 6, 4, 4, 7, 8, 4)
  
# printing original tuple
print("The original tuple : " + str(test_tuple))
  
# initializing K 
K = 4
  
# Remove first occurrence of K in Tuple
# Using index() + loop + list slicing
try:
    idx = test_tuple.index(K)
    res = test_tuple[:idx] + test_tuple[idx + 1:]
except ValueError:  
    res = test_tuple
  
# printing result 
print("Tuple after element removal : " + str(res))

chevron_right


Output :



The original tuple : (5, 6, 4, 4, 7, 8, 4)
Tuple after element removal : (5, 6, 4, 7, 8, 4)

 

Method #2 : Using enumerate() + generator expression
This is one of the ways in which this task can be performed. This offers one liner way to solve this problem. In this, we perform the task of checking for element and index using enumerate().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Remove first occurrence of K in Tuple
# Using enumerate() + generator expression
  
# initializing tuples
test_tuple = (5, 6, 4, 4, 7, 8, 4)
  
# printing original tuple
print("The original tuple : " + str(test_tuple))
  
# initializing K 
K = 4
  
# Remove first occurrence of K in Tuple
# Using enumerate() + generator expression
res = tuple(ele for idx, ele in enumerate(test_tuple) if idx != test_tuple.index(K))
  
# printing result 
print("Tuple after element removal : " + str(res))

chevron_right


Output :

The original tuple : (5, 6, 4, 4, 7, 8, 4)
Tuple after element removal : (5, 6, 4, 7, 8, 4)

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.