Open In App

Python | Removing duplicates from tuple

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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


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




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


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




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


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




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


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




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


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.



Previous Article
Next Article

Similar Reads

Python | Replace duplicates in tuple
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
5 min read
Python | Remove duplicates based on Kth element tuple list
Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop This is a brute-force method to perform this particu
8 min read
Python - Remove Equilength and Equisum Tuple Duplicates
Sometimes, while working with Python tuples, we can have a problem in which we need to remove duplicates on basis of equal length and equal sum. This kind of problem can also be broken to accommodate any one of the required conditions. This kind of problem can occur in data domains and day-day programming. Let's discuss certain ways in which this t
7 min read
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 performed. Input : test_list = [(4, 5, 6), (2, 3, 4), (1,
7 min read
Python Program For Removing All Occurrences Of Duplicates From A Sorted Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list. Examples: Input: 23->28->28->35->49->49->53->53 Output: 23->35 Input: 11->11->11->11->75->75 Output: empty List Note that this is different from Remove Duplica
3 min read
Python Program For Removing Duplicates From A Sorted Linked List
Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60. Recommended: Please solve it on "PRACTICE"
7 min read
Python Program For Removing Duplicates From An Unsorted Linked List
Write a removeDuplicates() function that takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43. Recommended: Please solve it on "PRACTICE" first, before moving
4 min read
Removing Duplicates of a List of Sets in Python
Dealing with sets in Python allows for efficient handling of unique elements, but when working with a list of sets, you may encounter scenarios where duplicates need to be removed. In this article, we will see how we can remove duplicates of a list of sets in Python. Remove Duplicates of a List of Sets in PythonBelow are some the ways by which we c
2 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Practice Tags :
three90RightbarBannerImg