views:

393

answers:

3

Hello,

I receive a dictionary as input, and would like to to return a dictionary whose keys will be the input's values and whose value will be the corresponding input keys. Values are unique.

For example, say my input is:

a = dict()
a['one']=1
a['two']=2

I would like my output to be:

{1: 'one', 2: 'two'}

To clarify I would like my result to be the equivalent of the following:

res = dict()
res[1] = 'one'
res[2] = 'two'

Any neat Pythonian way to achieve this?

Thanks

+6  A: 

res = dict(zip(a.values(), a.keys()))

pkit
dict does not guarantee that its values() and keys() will return elements in the same order. Also, keys(), values() and zip() return a list, where an iterator would be sufficient.
liori
You are right. Your answer is more efficient.
pkit
@liori: You're wrong. dict guarantees that its values() and keys() WILL be on the same order, if you don't modify the dict between calls to values() and keys() of course. The documentation states that here: (read the "Note" part: http://docs.python.org/library/stdtypes.html#dict.items) "If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond."
nosklo
Ok, then I am wrong... I haven't checked the online docs. Thank you for pointing this.
liori
You could use the iterator itertools.izip instead of zip to make this answer more efficient.
Alasdair
And iterkeys and itervalues. But could just as well use iteritems()
nosklo
nosklo, what if another thread modifies the dictionary?
liori
+14  A: 
res = dict((v,k) for k,v in a.iteritems())
liori
@nosklo, if you think about it, you'll realize that "24 minutes ago" was *earlier* than "22 minutes ago".
Armandas
@Armandas: huh, you're right. *hides*
nosklo
+7  A: 

You could try:

d={'one':1,'two':2}
d2=dict((value,key) for key,value in d.iteritems())
d2
  {'two': 2, 'one': 1}

Beware that you cannot 'reverse' a dictionary if

  1. More than one key shares the same value. For example {'one':1,'two':1}. The new dictionary can only have one item with key 1.
  2. One or more of the values is unhashable. For example {'one':[1]}. [1] is a valid value but not a valid key.

See this thread on the python mailing list for a discussion on the subject.

Alasdair
liori already provided that solution, but thanks
Roee Adler
+1 I added a note about values being unique, thanks
Roee Adler
@Alasdair: Right, I got confused with the timestamps
nosklo