Python Dictionary Key/Value Swap

0
13KB

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
The Help (2011)
An aspiring author during the civil rights movement of the 1960s decides to write a book...
Por Leonard Pokrovski 2023-06-29 19:58:48 0 33KB
Business
How Do I End a Presentation?
Ending a presentation is just as important as starting one — and in many cases, even more...
Por Dacey Rankins 2025-12-04 16:23:11 0 3KB
Business
How Do I Write a Good Prospecting Email?
Prospecting emails are one of the most powerful ways to start conversations, book meetings, and...
Por Dacey Rankins 2025-12-08 18:51:44 0 3KB
Contests
Contemporary
  The 5th season of the art competition from the gallery in London. Applications are...
Por FWhoop Xelqua 2023-03-11 19:45:33 0 23KB
Business
Why is Sustainability Important?
In recent years, the concept of sustainability has gained immense importance in discussions...
Por Dacey Rankins 2025-02-03 16:00:22 0 15KB

BigMoney.VIP Powered by Hosting Pokrov