Remove duplicates tuple in list of tuples Python

In this article, we are going to learn Way to remove duplicate tuples in list of tuples in Python. We will learn different techniques to do the clean-up by removing duplicate items.

1. List comprehension to Remove duplicates tuple in list of tuples Python


Let us start with our example where we have a list of tuples that contain duplicate items. We will use set() function to remove the duplicates and make a list of tuple unique using list compreshesion. The set only contains unique elements so by converting a list to set we can remove duplicate

Python Program Example

list_of_tuples = [("C#", 1), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)] 

#removing duplicate from list of tuples

unique_list_of_tuples = list(set([item for item  in list_of_tuples]))

print('list of tuple after removing duplicate=\n', unique_list_of_tuples)

Output

list of tuple after removing duplicate =
 [('Python', 25), ('Rust', 30), ('C#', 1), ('C++', 3)]

2. tuple() and List comprehension to Remove duplicates in list of tuples Python


In this example use list comprehension along with for loop and set to remove duplicate from list of tuples in Python.

Python Program to remove duplicate from list of tuples

list_of_tuples= [("C#", 1), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)]

result_lst = [tup for tup in (set(tuple(item) for item in list_of_tuples ))]



print(result_lst)

Output:

list of tuple after removing duplicate=
 (('Rust', 30), ('C++', 3), ('C#', 1), ('Python', 25))

3. OrderedDict() function to Remove duplicates tuple in list of tuples


In this example, we will use OrderedDict() function from Python the collections module. We are using items() to get the list of tuples after removing duplicates. Let’s understand with examples

Python Program to find a duplicate list of tuples

from collections import OrderedDict  

list_of_tuples= [("C#", 1), ("C#", 1), ("C++", 3),  
     ("C++", 3), ("Python", 25), ("Rust", 30)] 

print("original list of tuple:\n",list_of_tuples ) 
  
# OrderedDict to remove duplicate
unique_list = OrderedDict(list_of_tuples).items() 
print("\n after removing duplicate \n", unique_list)

Output:

original list of tuple:
 [('C#', 1), ('C#', 1), ('C++', 3), ('C++', 3), ('Python', 25), ('Rust', 30)]

after removing duplicate 
 odict_items([('C#', 1), ('C++', 3), ('Python', 25), ('Rust', 30)])

4. Enumerate() to Remove duplicates tuple in list of tuples Python


In this example, we are using enumerate() method to get a unique list of tuples after removing duplicate tuples from it.

Python Program Example

# Python3 program to remove duplicate tuples from list of tuples


list_of_tuples= [("C#", 1), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)]
result_lst = [[x, y] for item, [x, y] in enumerate(list_of_tuples) 
    if not any(z == y for _, z in list_of_tuples[:item])]

print(result_lst) 


Output

[('C#', 1), ('C++', 3), ('Python', 25), ('Rust', 30)]

Conclusion

We have understood Way to remove duplicates tuple in list of tuples Python tuples in Python with code examples that include list comprehension, enumerate() method, and orderdict().