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'}
Pesquisar
Categorias
Leia Mais
Social Issues
Goodfellas. (1990)
The story of Henry Hill and his life in the mob, covering his relationship with his wife Karen...
Por Leonard Pokrovski 2022-11-14 19:06:17 0 30K
Boxing
The Art and Sport of Boxing: A Brief Overview
Boxing, often referred to as the "sweet science," is one of the oldest and most celebrated combat...
Por Dacey Rankins 2024-11-07 16:28:32 0 12K
Marketing and Advertising
Can I Make Money Blogging? (The Complete Guide to Monetizing Your Blog)
Many aspiring bloggers ask the same question: 👉 Can blogging actually make money, or is it just...
Por Dacey Rankins 2025-09-23 17:12:24 0 5K
Business and Society
10 Tips for a New Entrepreneur
An entrepreneur differs from the rest not only in his activity, but also in his consciousness,...
Por Nikolai Pokryshkin 2022-08-03 21:24:24 0 49K
Спорт
Большой Лебовски. The Big Lebowski. (1998)
Лос-Анджелес, 1991 год, война в Персидском заливе. Главный герой по прозвищу Чувак считает себя...
Por Nikolai Pokryshkin 2023-04-12 17:23:30 0 31K

BigMoney.VIP Powered by Hosting Pokrov