Python | Removing duplicates from tuple
Last Updated :
13 Mar, 2023
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() + tuple() This is the most straight forward way to remove duplicates. In this, we convert the tuple to a set, removing duplicates and then converting it back again using tuple().
Python3
test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 )
print ("The original tuple is : " + str (test_tup))
res = tuple ( set (test_tup))
print ("The tuple after removing duplicates : " + str (res))
|
Output
The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
The tuple after removing duplicates : (1, 3, 5, 2)
Time complexity: O(n), where n is the length of the input tuple.
Auxiliary space: O(n), where n is the length of the input tuple.
Method #2 : Using OrderedDict() + fromkeys()
The combination of the above functions can also be used to perform this particular task. In this, we convert the tuples to dictionaries removing duplicates and then accessing its keys.
Python3
from collections import OrderedDict
test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 )
print ("The original tuple is : " + str (test_tup))
res = tuple (OrderedDict.fromkeys(test_tup).keys())
print ("The tuple after removing duplicates : " + str (res))
|
Output
The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
The tuple after removing duplicates : (1, 3, 5, 2)
Time complexity: O(n), where n is the length of the input tuple.
Auxiliary space: O(n), where n is the length of the input tuple.
Method #3: Using in, not in operators and tuple()
Python3
test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 )
print ( "The original tuple is : " + str (test_tup))
x = []
for i in test_tup:
if i not in x:
x.append(i)
res = tuple (x)
print ( "The tuple after removing duplicates : " + str (res))
|
Output
The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
The tuple after removing duplicates : (1, 3, 5, 2)
Time complexity: O(n), where n is the length of the input tuple.
Auxiliary space: O(n), where n is the length of the input tuple.
Method#4: Using list comprehension
Python3
test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 )
print ( "The original tuple is : " + str (test_tup))
res = tuple ([x for i, x in enumerate (test_tup) if x not in test_tup[:i]])
print ( "The tuple after removing duplicates : " + str (res))
|
Output
The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
The tuple after removing duplicates : (1, 3, 5, 2)
Time complexity: O(n)
Auxiliary Space: O(n)
Method 5 : Using Counter() from collections module
In this method, we use the Counter() function from the collections module to count the occurrences of each element in the tuple, and then convert the dictionary keys to a tuple to get the unique elements.
Python3
from collections import Counter
test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 )
print ( "The original tuple is : " + str (test_tup))
res = tuple (Counter(test_tup).keys())
print ( "The tuple after removing duplicates : " + str (res))
|
Output
The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
The tuple after removing duplicates : (1, 3, 5, 2)
Time complexity: O(n), where n is the number of elements in the tuple.
Auxiliary space: O(k), where k is the number of distinct elements in the tuple.
Please Login to comment...