Jump to content

[SOLVED] Python equivalent to PHP array_search


Recommended Posts

Mehh. I'm just about ready to kick the ass of the guy who wrote the python manual. Why is there no obvious list/dictionary method list? Why is there no proper search?

 

Anyone know what the python equivalent of PHP's array_search is? Basically trying to get a key according to a value, rather than value according to a key.

you could do something like this

 

def find_key(dic, val):
      return [k for k, v in dic.iteritems() if v == val][0]

 

and use it like

 

foo = {
'key1' : 'value1',
'key2' : 'value2'
}

print find_key(foo, 'value1') # prints key1

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.