Open In App

Python – Remove first occurrence of K in Tuple

Improve
Improve
Like Article
Like
Save
Share
Report

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)

Time complexity: O(n), where n is the number of elements in the tuple.
Auxiliary space complexity: O(n), as the res list is created with the same number of elements as the original tuple.

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)

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of tuple.

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)

Method#4: Using Recursion

Python3




def check(l,i,res,k,f):
  if i == len(l):
    return tuple(res)
  if l[i] == k:
    if f:
      res.append(l[i])
    else:
      f = 1
  else:
     res.append(l[i])
  return check(l,i+1,res,k,f)
# 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
f = 0
res = []
ans = check(test_tuple,0,res,k,f)
# printing result
print("Tuple after element removal : " + str(ans))
#This code is contributed Vinay Pinjala.


Output

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

Time complexity: O(n)
Auxiliary Space: O(n)

Method #5 : Using index(),list(),pop(),tuple() methods

Approach 

  1. Found the index of K using index() method
  2. Converted tuple to list using list() method
  3. Used pop() method and passed index as argument to remove the first occurrence of K
  4. And converted list to tuple again using tuple() method

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
a=test_tuple.index(K)
test_list=list(test_tuple)
test_list.pop(a)
res=tuple(test_list)
 
 
# 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)

Time Complexity : O(N)
Auxiliary Space : O(1)

Method #6: Using list comprehension and tuple unpacking.

Creates a list using list comprehension to iterate over the test_tuple and filters out the first occurrence of K using enumerate() and index() method. The resulting list is then converted back to a tuple using tuple() method.

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
res = tuple([x for i, x in enumerate(test_tuple) if x != K or i != 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)

Time complexity: O(n) 
Auxiliary space: O(n).

Method #7: Using list() and filter() method

  • Convert the tuple into a list.
  • Use the filter() method to remove the first occurrence of K in the list.
  • Convert the modified list back to a tuple.

Python3




# Python3 code to demonstrate working of
# Remove first occurrence of K in Tuple
# Using list() and filter() method
 
# 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 list() and filter() method
lst = list(test_tuple)
lst.remove(K)
res = tuple(lst)
 
# 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)

Time complexity: O(n)
Auxiliary space: O(n)

Method #8: Using tuple() constructor and slicing

Here’s the step by step approach for this method:

Initialize the original tuple and K.
Use slicing to create two new tuples: one that includes all elements before the first occurrence of K, and one that includes all elements after the first occurrence of K.
Concatenate the two new tuples using the tuple() constructor.
Print the original tuple and the updated tuple.

Python




# Python3 code to demonstrate working of
# Remove first occurrence of K in Tuple
# Using tuple() constructor and 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 tuple() constructor and slicing
index = test_tuple.index(K)
res = tuple(test_tuple[:index] + test_tuple[index+1:])
 
# 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)

time complexity of this method is O(n) and the space complexity is O(n).



Last Updated : 24 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads