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'}
Search
Categories
Read More
Programming
Proxy Webserver in Python
Socket programming in python is very user friendly as compared to c. The programmer need not...
By Jesse Thomas 2023-03-13 22:09:22 0 6K
Retail Trade
Understanding Retail Trade: A Comprehensive Overview
Retail trade plays a vital role in the economy, serving as the final link in the distribution...
By Dacey Rankins 2024-10-11 15:34:22 0 18K
Business
What is the difference between financial forecasting and budgeting?
In the world of business finance, financial forecasting and budgeting are two essential tools...
By Dacey Rankins 2025-01-10 14:19:37 0 3K
Life Issues
Colombiana. (2011)
A young girl in Bogotá witnesses her parents' murder and grows up to be a stone-cold...
By Leonard Pokrovski 2023-05-22 20:12:57 0 25K
Телевидение
Первый Музыкальный канал, прямой эфир. Беларусь
«Первый музыкальный» — музыкальный телеканал из Беларуси. Телеканал транслирует...
By Nikolai Pokryshkin 2022-11-20 22:18:19 0 25K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov