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
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts