Skip to content
Advertisement

How to remove duplicates in a list of tuples

I have a list of tuples:

JavaScript

So when the first item of the tuples in the list is double or more, i want to remove the duplicates. The end result has to be the wantedList. In a normal list i can do the “make a dictionary” trick but i dont know how to solve it with a list of tuples.

Advertisement

Answer

You can proceed with dict comprehension :

JavaScript

result is :

JavaScript

If you want to keep the first occurence, you can use reversed :

JavaScript

Which gives you :

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement