How to know the position of items in a Python ordered dictionary

You may get a list of keys with the keys property:

In [20]: d=OrderedDict((("fruit", "banana"), ("drinks", 'water'), ("animal", "cat")))

In [21]: d.keys().index('animal')
Out[21]: 2

Better performance could be achieved with the use of iterkeys() though.

For those using Python 3:

>>> list(d.keys()).index('animal')
2

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)