Dict comprehension
people = {
'Foo': 123,
'Bar': 456,
'SnowWhite': 7,
}
doubles = { k:v*2 for (k, v) in people.items() }
print(doubles) # {'Foo': 246, 'Bar': 912, 'SnowWhite': 14}
Output:
Press ← or → to navigate between chapters
Press S or / to search in the book
Press ? to show this help
Press Esc to hide this help
people = {
'Foo': 123,
'Bar': 456,
'SnowWhite': 7,
}
doubles = { k:v*2 for (k, v) in people.items() }
print(doubles) # {'Foo': 246, 'Bar': 912, 'SnowWhite': 14}
Output: