How to invert keys and values in a dictionary

It is very easy to invert keys and values with a list comprehension in python. Here is how.

Image
That’s how to invert keys and values in a dictionary

Thanks to pybytes

Here’s a more concise example

ad = {
	1 : "one",
	2 : "two"
}

b = {a: b for b, a in ad.items()}
print(b)

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.