Remove Duplicate Elements From a Tuple in Python

In this tutorial, we will learn how to remove duplicate elements from a tuple in Python. Sometimes in Python tuple, the data or objects are double and triple repeated, that repeated elements are called duplicate elements.

What is a tuple?

A tuple is a collection of objects. Tuples are immutable. Tuples Cannot change like a list and tuples are uses parentheses.

Python Programme To Remove Duplicate Elements From Tuple

In Python tuple sometimes elements or objects are repeated, these repeated elements and objects are duplicate elements in Python tuple.

Here we show  one example of how to remove duplicate elements from a tuple in a Python:


In the given example, we take a my_tuple variable that holds the elements inside the parenthesis. Given  tuple output is :

('jan', 'feb', 'mar', 'apr', 'jan', 'feb')

In this example, we show the repeated elements, those repeated elements are a duplicate element.

Here we show using the list comprehension plus set() method is used to how to remove these duplicate elements from the tuple list.

Given  Example, we take one set() method is b. We also take one variable result that holds the entire for loop and loop condition to check whether the given my_tuple storing element is added in the b set() function. If The giving my_tuple is stored again and again repeated element then it is not repeated in b set() and throughout the duplicate element in giving tuple. Below the given example we use slice operation to print the whole list is given.

Here is the example of removing a duplicate element from tuple:


result=[element for element in my_tuple
    if not (tuple(element) in b
        or  b.add(tuple(element)))]

The given example has output is :

('jan', 'feb', 'mar', 'apr', 'jan', 'feb')
['jan', 'feb', 'mar', 'apr']

Here we show output both including duplicate elements and removing duplicate elements from a tuple in a Python. The first output is given duplicate elements but the second output is given removing duplicate elements. The removing duplicate elements coming inside the square bracket because of using slice operation.

Slice operation is containing all the elements inside the index.


One response to “Remove Duplicate Elements From a Tuple in Python”

  1. Sreenikethan I says:

    Why can’t we do
    result = tuple(set(my_tuple))

    It will automatically eliminate the duplicates!

Leave a Reply