1

I have this dictionary here:

dictionary = {
'cat':3,
'dog':8,
'mouse':4
}

I wish to do something along the lines of saying "dictionary[1]" and having it return the value 8. I would like to do this without having to change the keys of the dictionary. Is there a clean way to do this? Thanks!

brubrudsi
  • 39
  • 3
  • Have a look at [collections OrderedDict](https://docs.python.org/3.7/library/collections.html#collections.OrderedDict) – match Mar 12 '19 at 21:24
  • A less outdated possible duplicate: [Why do dict_items objects not support indexing?](https://stackoverflow.com/q/52900328/674039) and also related is [Accessing dictionary items by position in Python 3.6+ efficiently](https://stackoverflow.com/q/52507860/674039) – wim Mar 12 '19 at 21:25
  • a friendly remind, dict keys are not ordered. – tim Mar 12 '19 at 21:28
  • 1
    @tim Actually, they are now (Python 3.7+). But the short answer is still, no, there is not any clean way. You'd have to use a list of pairs instead of a dict if you need efficient access by index. – wim Mar 12 '19 at 21:32

0 Answers0