I want to search a tuple of tuples for a particular string and return the index of the parent tuple. I seem to run into variations of this kind of search frequently.
What is the most pythonic way to do this?
I.E:
derp = (('Cat','Pet'),('Dog','Pet'),('Spock','Vulcan'))
i = None
for index, item in enumerate(derp):
if item[0] == 'Spock':
i = index
break
>>>print i
2
I could generalize this into a small utility function that takes an iterable, an index (I've hard coded 0 in the example) and a search value. It does the trick but I've got this notion that there's probably a one-liner for it ;)
I.E:
def pluck(iterable, key, value):
for index, item in enumerate(iterable):
if item[key] == value:
return index
return None