Python Dictionary Key/Value Swap

0
14K

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
Economics
What are the major global markets?
What Are the Major Global Markets? The World’s Economic Engine Room Is Bigger Than Most...
By Leonard Pokrovski 2026-06-04 23:35:16 0 5K
Business
What Is the Difference Between PaaS and SaaS?
The Question That Revealed a Bigger Strategy A few years ago, I was facilitating a workshop with...
By Dacey Rankins 2026-07-06 08:28:58 0 706
Business
How Do CEOs Foster a Positive Organizational Culture?
A company’s culture—the shared values, beliefs, and behaviors that shape how work...
By Dacey Rankins 2025-06-16 15:01:20 0 8K
Marketing and Advertising
What Are the Pros and Cons of Billboard Advertising?
Billboard advertising is one of the oldest forms of marketing, yet it continues to be relevant in...
By Dacey Rankins 2026-01-16 16:23:20 0 5K
История
Гладиатор. Gladiator. (2000)
В великой Римской империи не было военачальника, равного генералу Максимусу. Непобедимые легионы,...
By Nikolai Pokryshkin 2022-11-28 20:46:11 0 47K

BigMoney.VIP Powered by Hosting Pokrov