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

Опубликовано: 31 Март 2025
на канале: PatchyPython
73
0

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, see the notebooks below (notebooks will open on Google Colab - no installation required)

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