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
# Python3 code to demonstrate working of # Removing duplicates from tuple # using tuple() + set() # initialize tuple test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 ) # printing original tuple print ("The original tuple is : " + str (test_tup)) # Removing duplicates from tuple # using tuple() + set() res = tuple ( set (test_tup)) # printing result print ("The tuple after removing duplicates : " + str (res)) |
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
# Python3 code to demonstrate working of # Removing duplicates from tuple # using OrderedDict() + fromkeys() from collections import OrderedDict # initialize tuple test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 ) # printing original tuple print ("The original tuple is : " + str (test_tup)) # Removing duplicates from tuple # using OrderedDict() + fromkeys() res = tuple (OrderedDict.fromkeys(test_tup).keys()) # printing result print ("The tuple after removing duplicates : " + str (res)) |
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
# Python3 code to demonstrate working of # Removing duplicates from tuple # initialize tuple test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 ) # printing original tuple print ( "The original tuple is : " + str (test_tup)) # Removing duplicates from tuple x = [] for i in test_tup: if i not in x: x.append(i) res = tuple (x) # printing result print ( "The tuple after removing duplicates : " + str (res)) |
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
# Python3 code to demonstrate working of # Removing duplicates from tuple using list comprehension # initialize tuple test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 ) # printing original tuple print ( "The original tuple is : " + str (test_tup)) # Removing duplicates from tuple using list comprehension # creating a list of only unique elements from the tuple res = tuple ([x for i, x in enumerate (test_tup) if x not in test_tup[:i]]) # printing result print ( "The tuple after removing duplicates : " + str (res)) #This code is contributed by Vinay Pinjala. |
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
# Python3 code to demonstrate working of # Removing duplicates from tuple using Counter() from collections module # import Counter from collections module from collections import Counter # initialize tuple test_tup = ( 1 , 3 , 5 , 2 , 3 , 5 , 1 , 1 , 3 ) # printing original tuple print ( "The original tuple is : " + str (test_tup)) # Removing duplicates from tuple using Counter() from collections module # creating a tuple from Counter dictionary keys res = tuple (Counter(test_tup).keys()) # printing result print ( "The tuple after removing duplicates : " + str (res)) |
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.