I want to search tuple of tuplesfor a specific row and return the index of the parent tuple. I seem to often come across variations of this kind of search.
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 to a small utility function that takes iterability, an index (in the example I am hard-coded 0), and a search value. This does the trick, but I have an opinion that for him, probably, a single-line interface;)
i.e:
def pluck(iterable, key, value):
for index, item in enumerate(iterable):
if item[key] == value:
return index
return None
source
share