Python Dictionary Key/Value Swap

0
12KB

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
Marketing and Advertising
What Is Earned Media?
Understanding Its Value, Role, and Impact in Modern Public Relations In the landscape of modern...
Por Dacey Rankins 2025-10-30 16:32:11 0 3KB
Employment
The Evolving Landscape of Employment in the Modern World
Employment is a fundamental aspect of modern society, shaping the economy, providing individuals...
Por Dacey Rankins 2025-01-23 15:12:11 0 12KB
Financial Services
Beyond GDP: other ways to measure the economy
Key points Gross national product, or GNP, includes what is produced...
Por Mark Lorenzo 2023-03-03 19:43:30 0 20KB
Природа и наука
Том и Джерри. Tom & Jerry. (2021)
Кайла, сотрудница престижного отеля, где обитает мышонок Джерри, рискующий нарушить ход дорогой...
Por Nikolai Pokryshkin 2022-09-17 20:24:11 0 28KB
Arts, Culture and Entertainment
Annette. (2021)
Los Angeles, nowadays. Henry is a stand-up comedian with a fierce humor. Ann, an internationally...
Por Nikolai Pokryshkin 2022-09-01 20:11:00 1 43KB

BigMoney.VIP Powered by Hosting Pokrov