Posted By

Voil on 12/08/10


Tagged

dictionary


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

magna


Swapping keys and values of a dictionary


 / Published in: Python
 

URL: http://diveintopython3.org/comprehensions.html#stupiddicttricks

Based on the example by Mark Pilgrim.

  1. dict = {'a': 1, 'b': 2, 'c': 3}
  2. print({value:key for key, value in dict.items()})
  3. # Prints: {1: 'a', 2: 'b', 3: 'c'}

Report this snippet  

You need to login to post a comment.