Skip to content
Related Articles

Related Articles

Python – Remove first occurrence of K in Tuple

View Discussion
Improve Article
Save Article
  • Last Updated : 17 Aug, 2022
View Discussion
Improve Article
Save Article

Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of the first occurrence of an element in tuple. This type of problem can have applications 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 the 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. 

Python3




# 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))

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(). 

Python3




# 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))

Output : 

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

Method #3 : Using list(),map(),tuple(),join(),replace() methods

Python3




# Python3 code to demonstrate working of
# Remove first occurrence of K in Tuple
 
# 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
x = list(map(str, test_tuple))
p = "".join(x)
p = p.replace(str(K), "", 1)
y = list(map(int, p))
res = tuple(y)
 
# printing result
print("Tuple after element removal : " + str(res))

Output

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!