Remove duplicates tuple in list of tuples Python

Tuple

In this article, we are going to learn Way toRemove duplicates tuple in list of tuples 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 unique items.The set only contain unique elements so by converting a list to set we can remove duplicate

Python Progrm 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 compreshension to Remove duplicates tuple in list of tuples


In this example using 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 understand with examples

Python Program to find 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() method to Remove duplicates tuple in list of tuples Python duplicate


In this example we are using enumerate() method to get unique list of tuples after removing duplicate tuple 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 toRemove duplicates tuple in list of tuples Python tuples in Python with code examples that includes list comprehension,enumerate() method and orderdict().