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'}
Search
Categories
Read More
Научная фантастика и фэнтези
Интерстеллар. Interstellar. (2014)
Когда засуха, пыльные бури и вымирание растений приводят человечество к продовольственному...
By Nikolai Pokryshkin 2022-11-19 17:29:10 0 36K
Marketing and Advertising
What Makes a Billboard Memorable?
Thousands of billboards line highways, streets, and city centers, yet only a small fraction are...
By Dacey Rankins 2026-01-15 17:26:38 0 2K
Business and Corporate Finance
What Is the Difference Between a CFO and a CEO?
  What Is the Difference Between a CFO and a CEO? In any successful organization,...
By Leonard Pokrovski 2026-01-11 05:51:25 0 2K
Finance
Which Finance Biography Books Are More Suitable for Beginners vs. More Advanced Readers?
Which Finance Biography Books Are More Suitable for Beginners vs. More Advanced Readers? Finance...
By Leonard Pokrovski 2025-10-08 20:56:28 0 5K
Телевидение
Первый Музыкальный канал, прямой эфир. Беларусь
«Первый музыкальный» — музыкальный телеканал из Беларуси. Телеканал транслирует...
By Nikolai Pokryshkin 2022-11-20 22:18:19 0 39K

BigMoney.VIP Powered by Hosting Pokrov