(TIL) Python: Invert a mapping

less than 1 minute read

Given this dictionary:

my_map = { 'a': 1, 'b':2 }

you can invert it:

inv_map = {v: k for k, v in my_map.items()}

to get:

$ inv_map
{ 1: 'a', 2: 'b' }

Via StackOverflow.com.

Comments