Swapping keys and values of a dictionary


/ Published in: Python
Save to your folder(s)

Based on the example by [Mark Pilgrim](http://diveintopython3.org/about.html).


Copy this code and paste it in your HTML
  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'}

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.