Python Dictionary Key/Value Swap

0
13K

You can change the keys and values in a dictionary using a loop and items(). This can be helpful if the key doesn't exist in the dictionary, but the value exist. Here's an example:

d = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

d_swap = {v: k for k, v in d.items()}
print(d_swap)
# {'val1': 'key1', 'val2': 'key2', 'val3': 'key3'}
Zoeken
Categorieën
Read More
Programming
Express.JS
What Is Express JS?      Express is a node js web application framework...
By Jesse Thomas 2023-05-24 21:10:49 0 11K
Television
The Mercedes Wilson Show. Live TV. USA.
Mercedes E. Wilson is a transformational speaker, author, television personality, event emcee,...
By Nikolai Pokryshkin 2022-10-07 17:37:07 0 29K
Научная фантастика и фэнтези
Властелин колец: Возвращение короля. The Lord of the Rings: The Return of the King. (2003)
Повелитель сил тьмы Саурон направляет свою бесчисленную армию под стены Минас-Тирита, крепости...
By Nikolai Pokryshkin 2022-11-03 20:37:27 0 42K
Business and Corporate Finance
What Skills Does a CFO Need?
What Skills Does a CFO Need? Financial, Strategic, Leadership, and Communication Skills The...
By Leonard Pokrovski 2026-01-12 18:55:44 0 1K
Programming
How the Python or Operator Works
With the Boolean OR operator, you can connect two Boolean expressions into one compound...
By Jesse Thomas 2023-03-06 23:53:35 0 12K

BigMoney.VIP Powered by Hosting Pokrov