Search code examples
pythonlisttuplesunique

Python - Tuples - Retrieve unique elements in list of tuples


UPDATE:

I went with this:

set(item[1] for item in id) 

Thanks guys, your ideas helped me.


I am working with a list of tuples:

Using the following line of code as an example. My list can be of any lenght. However, I will always be looking for a given index of my tuples:

id = [(9,'Tup','Check'),(10,'Tup','Pyton'),(11,'Not Tup','Stack'),(12,'Not Tup','Stack')]

In this scenario, I am looking to grab the unique second elements.

objective_ouput = ('Tup','Not Tup')

Solution

  • It as simple as follow:

    objective_ouput_set = {item[1] for item in id}