27

Possible Duplicate:
Inverse dictionary lookup - Python
reverse mapping of dictionary with Python

How do i get key of index in dictionary?

For example like:

i = {'a': 0, 'b': 1, 'c': 2}

so if i want to get key of i[0], it will return 'a'

Community
  • 1
  • 1
Natsume
  • 881
  • 3
  • 15
  • 22
  • You want to map the *value* back to the key. There is no index in a python dict. – Martijn Pieters Jul 24 '12 at 14:21
  • See http://stackoverflow.com/questions/3221475/reverse-mapping-of-dictionary-with-python and http://stackoverflow.com/questions/483666/python-reverse-inverse-a-mapping – Jeremy Brown Jul 24 '12 at 14:23

3 Answers3

34

You could do something like this:

i={'foo':'bar', 'baz':'huh?'}
keys=i.keys()  #in python 3, you'll need `list(i.keys())`
values=i.values()
print keys[values.index("bar")]  #'foo'

However, any time you change your dictionary, you'll need to update your keys,values because dictionaries are not ordered in versions of Python prior to 3.7. In these versions, any time you insert a new key/value pair, the order you thought you had goes away and is replaced by a new (more or less random) order. Therefore, asking for the index in a dictionary doesn't make sense.

As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. As of Python 3.7+ dictionaries are ordered by order of insertion.

Also note that what you're asking is probably not what you actually want. There is no guarantee that the inverse mapping in a dictionary is unique. In other words, you could have the following dictionary:

d={'i':1, 'j':1}

In that case, it is impossible to know whether you want i or j and in fact no answer here will be able to tell you which ('i' or 'j') will be picked (again, because dictionaries are unordered). What do you want to happen in that situation? You could get a list of acceptable keys ... but I'm guessing your fundamental understanding of dictionaries isn't quite right.

CervEd
  • 3,306
  • 28
  • 25
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I think your answer is very misleading. Dictionaries are unordered. You shouldn't post a work around to something that fundamentally defies programming logic. – Paul Seeb Jul 24 '12 at 14:30
  • 2
    @PaulSeeb -- And why do you think I added all the "What you're asking for is probably not what you want" and "I'm guessing your fundamental understanding of dictionaries isn't quite right"? – mgilson Jul 24 '12 at 14:36
  • Just that teaching bad habits/principles, even with an explanation saying why its bad, the person might very well just say, "well that works for me so ill use it this time". Better to teach the lesson and let them figure out the work around if that's really what they want. – Paul Seeb Jul 24 '12 at 18:30
21

Python dictionaries have a key and a value, what you are asking for is what key(s) point to a given value.

You can only do this in a loop:

[k for (k, v) in i.items() if v == 0]

Note that there can be more than one key per value in a dict; {'a': 0, 'b': 0} is perfectly legal.

As of Python 3.6 the dictionary implementation retains the order you inserted keys (it became part of the language specification in 3.7). However, if you need more control over the order you could use a list:

items = ['a', 'b', 'c']
items.index('a') # gives 0
items[0]         # gives 'a'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
6

By definition dictionaries are unordered, and therefore cannot be indexed. For that kind of functionality use an ordered dictionary. Python Ordered Dictionary

underbar
  • 588
  • 3
  • 15
  • `OrderedDict`s cannot be indexed either. – Julian Jul 24 '12 at 15:28
  • True, but when dict.keys() or dict.values() are called the order is preserved. So the index on these lists corresponds to the "index" of the dictionary – underbar Jul 24 '12 at 15:31
  • 3
    Python 3.7 Update: Dictionaries are ordered now: https://mail.python.org/pipermail/python-dev/2017-December/151283.html – four43 Sep 19 '18 at 20:23