Python Dictionary Key/Value Swap

0
14Кб

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'}
Поиск
Категории
Больше
Personal Finance
What’s Included in the Cost of Education?
What’s Included in the Cost of Education? Understanding Tuition and the Many Hidden Costs...
От Leonard Pokrovski 2025-10-30 22:32:55 0 11Кб
Социальные проблемы
Король Стейтен-Айленда. The King of Staten Island. (2020)
24-летний инфантил по имени Скотт живёт с мамой и младшей сестрой в нью-йоркском районе...
От Nikolai Pokryshkin 2022-10-16 17:00:48 0 37Кб
Economics
What percentage of wealth does the top 1% own?
What Percentage of Wealth Does the Top 1% Own? Wealth inequality has become one of the defining...
От Leonard Pokrovski 2026-07-07 01:00:33 0 306
Economics
Why is environmental economics important?
Why Environmental Economics Matters More Than Most Economists Admit The modern economist suffers...
От Leonard Pokrovski 2026-05-21 19:31:41 0 3Кб
Business
How to Grow a B2B Business? Most Companies Don’t Have a Growth Problem. They Have a Focus Problem.
A founder once told me his company was “doing everything right” and still not growing...
От Dacey Rankins 2026-05-22 17:36:12 0 5Кб

BigMoney.VIP Powered by Hosting Pokrov