Inverting a Python dictionary with zip(): swapping keys and values

Опубликовано: 21 Апрель 2026
на канале: PatchyPython
104
0

Try this code in your browser: click here
https://tinyurl.com/5cud7uzd

The zip function transforms two lists of the same length into a single list by "pairing up" corresponding elements into a tuple. Here, we use "zip" to "invert" a dictionary, by swapping its keys for its values and vice-versa. Note that since all the keys in a dictionary must obviously be different, this makes sense only if no two values are equal. For more information and to try out the code, click on the link above - a notebook will open in your browser, no need to install Python on your machine.

Embedded video, also part of the following courses:
Computer Programming at Queen Mary, University of London
FREE Colab notebook: https://tinyurl.com/3xjmpf94
Coding for Scientists at Queen Mary, University of London
FREE Colab notebook: https://tinyurl.com/yedumefr