Python Dictionary Key/Value Swap

0
13KB

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'}
Pesquisar
Categorias
Leia mais
Lumberjack
The Thrilling World of Lumberjack Sports: Blending Tradition with Competitive Spirit
Deep in the heart of rugged forests and amidst the echoes of swinging axes and roaring chainsaws,...
Por Dacey Rankins 2024-07-01 15:04:06 0 17KB
Business
How Can I Encourage Others to Give Me Feedback?
Creating Environments Where Open and Honest Feedback Is Welcomed and Valued Feedback is a...
Por Dacey Rankins 2025-05-28 14:23:33 0 14KB
Business
What Is the Difference Between a Merger and an Acquisition?
A Comprehensive 3,000-Word Guide to M&A Fundamentals Mergers and acquisitions —...
Por Dacey Rankins 2025-11-26 13:20:43 0 2KB
Running
The Art and Science of Sports Running: A Journey of Endurance and Achievement
Running, the fundamental act of propelling oneself swiftly on foot, transcends mere physical...
Por Dacey Rankins 2024-07-08 15:24:40 0 23KB
Business
Bloomberg: Plant closures threaten the entire EU auto industry
Volkswagen (VW)'s plans to close its factories in Germany for the first time in history are only...
Por Dacey Rankins 2024-09-11 13:54:57 0 15KB

BigMoney.VIP Powered by Hosting Pokrov