Python | Replace duplicates in tuple
Last Updated :
23 Mar, 2023
Sometimes, while working with Python tuples, we can have a problem in which we need to remove tuples elements that occur more than one times and replace duplicas with some custom value. Let's discuss certain ways in which this task can be performed.
Method #1 : Using set() + list comprehension
The combination of above functionalities can be used to perform this particular task. In this, we just initialize a set container and then replace the reoccurring elements with a value after checking its existence it's existence in tuple.
Python3
# Python3 code to demonstrate working of
# Replace duplicates in tuple
# using set() + list comprehension
# initialize tuple
test_tup = (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Replace duplicates in tuple
# using set() + list comprehension
temp = set()
res = tuple(ele if ele not in temp and not temp.add(ele)
else 'gfg' for ele in test_tup)
# printing result
print("Tuple after replacing values : " + str(res))
Output : The original tuple is : (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
Tuple after replacing values : (1, 'gfg', 4, 'gfg', 'gfg', 5, 'gfg', 6, 7, 'gfg')
Method #2 : Using groupby() + loop
The combination of above functionalities can be solved using this problem. In this, we just group the consecutive elements and then replace each element except for 1st with default value. Works only in case of consecutive duplicates.
Python3
# Python3 code to demonstrate working of
# Replace duplicates in tuple
# using groupby() + loop
from itertools import groupby
# initialize tuple
test_tup = (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Replace duplicates in tuple
# using groupby() + loop
res = tuple()
for key, ele in groupby(test_tup):
res = res + ((key, ) + ('gfg', ) * (len(list(ele))-1))
# printing result
print("Tuple after replacing values : " + str(res))
Output : The original tuple is : (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
Tuple after replacing values : (1, 'gfg', 4, 'gfg', 'gfg', 5, 'gfg', 6, 7, 'gfg')
Method#3:Using dict.fromkeys
Python3
# Python3 code to demonstrate working of
# Replace duplicates in tuple
# using dict.fromkeys and string value 'gfg'
# initialize tuple
test_tup = (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Replace duplicates in tuple
# using dict.fromkeys and string value 'gfg'
temp = set()
res = tuple('gfg' if ele in temp or temp.add(ele)
else ele for ele in test_tup)
# printing result
print("Tuple after replacing values : " + str(res))
#this code is contributed by Vinay pinjala.
OutputThe original tuple is : (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
Tuple after replacing values : (1, 'gfg', 4, 'gfg', 'gfg', 5, 'gfg', 6, 7, 'gfg')
Time complexity: O(n)
Auxiliary Space: O(n)
Method#4: Using Recursive method.
Algorithm:
- Define a function named "replace_duplicates" which takes the following parameters:
a) test_tup: The tuple which contains duplicate elements to be replaced.
b) replace_word: The word which will replace the duplicate element(s).
c) new_tup: A new tuple in which the replaced elements will be stored.
d) start: The index position to start searching for duplicates in the tuple. - Check if the length of the tuple is equal to the start index, if it is, then return the new tuple.
- Check if the current element at the start index is already present in the new tuple. If it is, then append the replace_word to the new tuple.
- If the element is not present in the new tuple, then append the element to the new tuple.
- Return the result of the function by recursively calling the function and incrementing the start index by 1.
- Initialize a tuple named "test_tup" with some duplicate elements.
- Print the original tuple.
- Call the "replace_duplicates" function and pass the "test_tup" tuple as the parameter.
- Print the resulting tuple after replacing the duplicates.
Python3
# Python3 code to demonstrate working of
# Replace duplicates in tuple
def replace_duplicates(test_tup,replace_word='gfg',new_tup=(),start=0):
if len(test_tup)==start:return new_tup
if test_tup[start] in new_tup:
new_tup=new_tup+(replace_word,)
else:
new_tup=new_tup+(test_tup[start],)
return replace_duplicates(test_tup,replace_word,new_tup,start+1)
# initialize tuple
test_tup = (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
# printing original tuple
print("The original tuple is : " + str(test_tup))
# Replace duplicates in tuple
res = replace_duplicates(test_tup)
# printing result
print("Tuple after replacing values : " + str(res))
OutputThe original tuple is : (1, 1, 4, 4, 4, 5, 5, 6, 7, 7)
Tuple after replacing values : (1, 'gfg', 4, 'gfg', 'gfg', 5, 'gfg', 6, 7, 'gfg')
Time Complexity: O(n^2) [due to checking for duplicates in the new tuple for every element in the original tuple]
Space Complexity: O(n) [due to creating a new tuple to store the replaced elements]
Similar Reads
Python - Remove Kth Index Duplicates in Tuple Sometimes, while working with Python records, we can have a problem in which we need to remove all the tuples, which have similar Kth index elements in list of records. This kind of problem is common in day-day and web development domain. Let's discuss certain ways in which this task can be performe
7 min read
Python | Removing duplicates from tuple Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 min read
Python - Remove Duplicate subset Tuples Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of tuples, which are already present as subsets in other tuples. This kind of problem can be useful in data preprocessing. Let's discuss certain ways in which this task can be performed. Exampl
6 min read
Python - Replace duplicate Occurrence in String Sometimes, while working with Python strings, we can have problem in which we need to perform the replace of a word. This is quite common task and has been discussed many times. But sometimes, the requirement is to replace occurrence of only duplicate, i.e from second occurrence. This has applicatio
6 min read
Python | Remove duplicate tuples from list of tuples Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x
5 min read
Python - Remove all duplicate occurring tuple records Sometimes, while working with records, we can have a problem of removing those records which occur more than once. This kind of application can occur in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + set() + count() Init
6 min read