Remove the duplicates from a tuple in Python

avatar

Borislav Hadzhiev

Last updated: Jun 30, 2022

banner

Photo from Unsplash

Remove the duplicates from a tuple in Python #

To remove the duplicates from a tuple:

  1. Use the set() class to convert the tuple to a set.
  2. Any duplicate values will automatically get removed after the conversion.
  3. Use the tuple() class to convert the set back to a tuple.
main.py
my_tuple = ('a', 'b', 'b', 'a', 'c', 'c') tuple_without_duplicates = tuple(set(my_tuple)) # 👇️ ('b', 'c', 'a') print(tuple_without_duplicates)

Set objects are an unordered collection of unique elements, so when we convert the tuple to a set, all duplicates are automatically removed.

One thing to note though - set objects are unordered, so you don't have a guarantee that the order of the items in the tuple is going to be preserved.

If you need to be sure that the order is going to be preserved, use the OrderedDict.fromkeys() method.

To remove the duplicates from a tuple:

  1. Use the OrderedDict.fromkeys() method to convert the tuple to an ordered dictionary.
  2. Converting the tuple to a dictionary removes any of the duplicates.
  3. Use the tuple() class to convert the dictionary back to a tuple.
main.py
from collections import OrderedDict my_tuple = ('a', 'b', 'b', 'a', 'c', 'c') # 👇️ OrderedDict([('a', None), ('b', None), ('c', None)]) print(OrderedDict.fromkeys(my_tuple)) tuple_without_duplicates = tuple(OrderedDict.fromkeys(my_tuple)) # 👇️ ('a', 'b', 'c') print(tuple_without_duplicates)

The OrderedDict class remembers the order in which its entries were added.

The fromkeys method takes an iterable and creates a new dictionary with keys from the iterable.

The method also takes a value argument, which if not specified defaults to None.

Since dictionary keys are unique, the conversion of tuple elements to dictionary keys automatically removes any duplicates.

The last step is to use the tuple() class to convert the ordered dictionary back to a tuple.

You can also use a simple for loop to remove the duplicates from a tuple.

main.py
my_tuple = ('a', 'b', 'b', 'a', 'c', 'c') my_list = [] for item in my_tuple: if item not in my_list: my_list.append(item) tuple_without_duplicates = tuple(my_list) # 👇️ ('a', 'b', 'c') print(tuple_without_duplicates)

The first step is to iterate over the tuple.

On each iteration, we check if the current item is not present in the new list.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

x not in l returns the negation of x in l.

If the condition is met, we use the list.append() method to add the item to the new list.

The list.append() method adds an item to the end of the list.

The last step is to use the tuple() class to convert the list back to a tuple.

We had to use a list because tuples are immutable (they cannot be changed).

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.