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
Business
Form of government
The form of state government is an element of the form of the state, which determines the system...
Por Dacey Rankins 2024-09-25 19:18:22 0 33K
Marketing and Advertising
Do I Need to Disclose That I’m Using Affiliate Links?
Affiliate marketing has grown into one of the most popular ways to monetize online content....
Por Dacey Rankins 2025-09-18 16:21:55 0 6K
Programming
asyncio.sleep() vs time.sleep()
Both of these functions basically has the same purpose, but a little different. Time.sleep()...
Por Jesse Thomas 2023-03-15 19:00:59 0 14K
E-Books
The Rise and Impact of E-books: Revolutionizing Reading in the Digital Age
In the digital age, e-books have transformed the way people read and interact with literature....
Por Dacey Rankins 2024-11-20 16:29:33 0 11K
Ужасы
Семь. Se7en. (1995)
Детектив Уильям Сомерсет - ветеран уголовного розыска, мечтающий уйти на пенсию и уехать подальше...
Por Nikolai Pokryshkin 2022-11-14 12:13:12 0 33K

BigMoney.VIP Powered by Hosting Pokrov