0

List Comprehension with Dictionaries

Is it possible to use list comprehension with dictionaries? (I'm assuming it's not possible with tuples)

2nd Jan 2017, 7:39 AM
Regi Nettey
Regi Nettey - avatar
1 Respuesta
+ 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)]))
2nd Jan 2017, 10:43 AM
richard