Skip to content
Advertisement

How to get the index with the key in a dictionary?

I have the key of a python dictionary and I want to get the corresponding index in the dictionary. Suppose I have the following dictionary,

JavaScript

Is there a combination of python functions so that I can get the index value of 1, given the key value ‘b’?

JavaScript

I know it can be achieved with a loop or lambda (with a loop embedded). Just thought there should be a more straightforward way.

Advertisement

Answer

Use OrderedDicts: http://docs.python.org/2/library/collections.html#collections.OrderedDict

JavaScript

For those using Python 3

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement