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'}
Search
Categories
Read More
Materials
Methodology for describing a business idea and working materials for the development of a business idea
Methodology for describing a business idea and working materials for the development of a...
By Leonard Pokrovski 2024-03-15 13:18:55 0 34K
Business
10 Examples of Gamification in Building Customer Relationships
What is relationship selling?The term "relationship sales" describes an approach that is the...
By Dacey Rankins 2024-09-09 19:37:26 0 14K
Marketing and Advertising
How Does a CMO Measure Marketing Effectiveness?
One of the most critical responsibilities of a Chief Marketing Officer (CMO) is proving the...
By Dacey Rankins 2025-09-25 15:39:27 0 5K
История
Взвод. Platoon. (1986)
В сентябре 1967 года куда-то в приграничный район между Вьетнамом и Камбоджей прибыл рядовой...
By Nikolai Pokryshkin 2023-04-16 17:55:42 0 32K
Business
How Does Viral Growth Work or What Drives It?
Viral growth is often described as the “holy grail” of scaling businesses. Instead of...
By Dacey Rankins 2025-09-17 10:45:24 0 4K

BigMoney.VIP Powered by Hosting Pokrov