Python Dictionary Key/Value Swap

0
12K

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
Lua vs JavaScript
Sr. No Lua JavaScript 1 Lua is one of the cross-platform programming languages...
By Jesse Thomas 2023-06-07 21:31:08 0 11K
Hand Games
16 of the best games of our childhood that we lost
In the XXI century, children quietly disappeared from the courtyards of large cities - now they...
By FWhoop Xelqua 2022-11-14 10:59:43 0 33K
Социальные проблемы
Элегия Хиллбилли. Hillbilly Elegy. (2020)
Исследование темы американской мечты на примере трёх поколений одной семьи, живущих в горах...
By Nikolai Pokryshkin 2022-10-06 22:16:57 0 28K
Social Issues
Die Hard. (1988)
A New York City police officer tries to save his estranged wife and several others taken hostage...
By Leonard Pokrovski 2023-01-31 14:35:24 0 26K
Financial Services
8 money tips for freelance workers
If you’ve traded in your cubicle to become a freelancer, you’re not alone. Fifteen...
By Mark Lorenzo 2023-05-18 19:49:49 0 28K

BigMoney.VIP Powered by Hosting Pokrov