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'}
Zoeken
Categorieën
Read More
Home Health
How pet dogs and cats improve human physical and mental health
How pet dogs and cats improve human physical and mental health Pets bring joy, love...
By Leonard Pokrovski 2024-04-16 19:43:43 0 23K
Business
Can Someone Become a CEO Without Certain Education Credentials—Like an MBA?
While many CEOs hold MBAs or advanced degrees, it is not a strict requirement for reaching the...
By Dacey Rankins 2025-08-14 21:38:27 0 5K
Personal Finance
How Much Can I Borrow and How Long Will I Have to Repay the Loan?
How Much Can I Borrow and How Long Will I Have to Repay the Loan? Understanding Loan Amounts,...
By Leonard Pokrovski 2025-11-05 15:58:48 0 368
Sport
Seaspiracy 2021
Passionate about ocean life, a filmmaker sets out to document the harm that humans do to marine...
By Leonard Pokrovski 2022-09-12 20:16:34 0 28K
Mental Health
ADHD: History
Hyperactivity has long been part of the human condition. Sir Alexander Crichton describes "mental...
By Kelsey Rodriguez 2023-05-08 19:41:00 0 13K

BigMoney.VIP Powered by Hosting Pokrov