Python Dictionary Key/Value Swap

0
12Кб

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'}
Поиск
Категории
Больше
Природа и наука
Лошадь Мечты. Dream Horse. (2021)
Устав от обыденной жизни, Джен решает ее изменить. Не имея ни опыта, ни средств, она убеждает...
От Nikolai Pokryshkin 2022-09-07 19:02:28 0 30Кб
Personal Finance
How Do I Create a Budget?
How Do I Create a Budget? Creating a budget is one of the most powerful steps you can take to...
От Leonard Pokrovski 2025-10-20 16:55:33 0 5Кб
Science Fiction and Fantasy
How to Train Your Dragon. (2010)
A hapless young Viking who aspires to hunt dragons becomes the unlikely friend of a young dragon...
От Leonard Pokrovski 2023-04-08 20:29:05 0 36Кб
Business
What is the difference between financial forecasting and budgeting?
In the world of business finance, financial forecasting and budgeting are two essential tools...
От Dacey Rankins 2025-01-10 14:19:37 0 11Кб
Marketing and Advertising
How to Increase Brand Recognition and the Role of a CMO
In today’s saturated marketplace, brand recognition is crucial for business success. A...
От Dacey Rankins 2025-09-25 17:41:37 0 5Кб

BigMoney.VIP Powered by Hosting Pokrov