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
Смотрите видео Inverting a Python dictionary with zip(): swapping keys and values онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь PatchyPython 13 Сентябрь 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 73 раз и оно понравилось 0 людям.