28

Possible Duplicate:
Python: Best Way to Exchange Keys with Values in a Dictionary?

Suppose that I need to swap keys for values in dictionary.

This is what I have in mind (Assuming that the values' value is unique):

>>> my_dict = {'x':1, 'y':2, 'z':3}
>>> my_dict2 = {}
>>> for key, val in my_dict.items():
        my_dict2[val] = key

Is there any other efficient way to do it?

Community
  • 1
  • 1
riza
  • 16,274
  • 7
  • 29
  • 29

0 Answers0