Python | Removing duplicates from tuple

Many times, while working with Python tuples, we can have a problem of removing duplicates. This is a very common problem and can occur in any form of programming set up, 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().

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
The tuple after removing duplicates : (1, 2, 3, 5)

 

Method #2 : Using OrderedDict() + fromkeys()
The combination of above functions can also be used to perform this particular task. In this, we convert the tuples to dictionary removing duplicates and then accessing it’s keys.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

The original tuple is : (1, 3, 5, 2, 3, 5, 1, 1, 3)
The tuple after removing duplicates : (1, 2, 3, 5)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.