Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren0
List Comprehension with Dictionaries
Is it possible to use list comprehension with dictionaries? (I'm assuming it's not possible with tuples)
1 Antwort
+ 1
# There are a lot of options for construction
# via dictionary comprehensions
# here's a simple example. Note the {} syntax and
# the : for specifying the key: value pair
my_d = {x:y for x,y in [('a',1),('b',2)]}
print(type(my_d))
print(my_d)
# comprehensions are not always necessary for simple cases
print(dict([('a',1),('b',2)]))