Equivalent of list.index() for an OrderedDict?

377 views Asked by At

Given that by its very nature, the items in an OrderedDict are ordered, one might expect it to have a function similar to the index function of lists. It does not.

What is the most Pythonic / neatest way of mimicing index for an OrderedDict?

1

There are 1 answers

1
lemonhead On BEST ANSWER

It depends what you want the index of. If you want the index of a key, you can just do

o = OrderedDict([('toast',5.80),('waffles',2.30),('pancakes',3.99)])
print o.keys().index('waffles')