I have a data structure which is a collection of tuples like this:
things = ( (123, 1, "Floogle"), (154, 33, "Blurgle"), (156, 55, "Blarg") )
The first and third elements are each unique to the collection.
What I want to do is retrieve a specific tuple by referring to the third value, eg:
>>> my_thing = things.get( value(3) == "Blurgle" )
(154, 33, "Blurgle")
There must be a better way than writing a loop to check each value one by one!
If
thingsis a list, and you know that the third element is uniqe, what about a list comprehension?Although under the hood, I assume that goes through all the values and checks them individually. If you don’t like that, what about changing the
my_thingsstructure so that it’s adictand using either the first or the third value as the key?