Python Dictionary Key/Value Swap

0
6K

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
Financial Services
Efficiency in perfectly competitive markets
Key points Long-run equilibrium in perfectly competitive markets meets two...
By Mark Lorenzo 2023-02-15 11:44:02 0 8K
Television
National Religious Broadcasters. Live TV. USA.
National Religious Broadcasters (NRB) is an international association of evangelical...
By Nikolai Pokryshkin 2022-10-09 11:41:51 0 18K
Other
What Is a Woman? (2022)
Political commentator Matt Walsh explores the changing concepts of sex and gender in the digital...
By Leonard Pokrovski 2023-07-08 20:42:48 0 17K
Visual Arts
Visual Arts
Pictorial art or pictorial arts (from "from" and Art.-Slav. "image"; praslav. ob- + razъ —...
By FWhoop Xelqua 2023-02-18 13:32:13 0 11K
Social Issues
Citizen Kane. (1941)
Following the death of publishing tycoon Charles Foster Kane, reporters scramble to uncover the...
By Leonard Pokrovski 2023-01-14 21:59:59 0 16K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov