Python Dictionary Key/Value Swap

0
13K

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
Социальные проблемы
Храброе сердце. Braveheart. (1995)
Действие фильма начинается в 1280 году в Шотландии. Это история легендарного национального героя...
Por Nikolai Pokryshkin 2023-01-01 18:28:38 0 24K
Marketing and Advertising
How Do I Set a SEM Budget?
Setting a search engine marketing (SEM) budget is one of the most important decisions...
Por Dacey Rankins 2026-02-19 17:25:52 0 2K
Marketing and Advertising
Telemarketing Explained: A Comprehensive Guide for Businesses and Consumers
Introduction Telemarketing remains one of the most widely used direct marketing strategies,...
Por Dacey Rankins 2025-11-14 23:47:41 0 6K
Business
Technologies for Small Business Growth: From Startup to Success
The impact of digital technologies on the growth of startupsDigital technologies play a key role...
Por Dacey Rankins 2024-09-11 13:52:25 0 17K
Stories
Come and See. (1985)
After finding an old rifle, a young boy joins the Soviet resistance movement against ruthless...
Por Leonard Pokrovski 2023-01-13 21:02:48 0 27K

BigMoney.VIP Powered by Hosting Pokrov