I like the python list comprehension operator (or idiom, or whatever it is).
Can it be used to create dictionaries too? For example, by iterating over pairs of keys and values:
dict = {(k,v) for (k,v) in blah blah blah} # doesn't work :(
I like the python list comprehension operator (or idiom, or whatever it is).
Can it be used to create dictionaries too? For example, by iterating over pairs of keys and values:
dict = {(k,v) for (k,v) in blah blah blah} # doesn't work :(
More or less, but use the dict constructor:
d = dict((k,v) for (k,v) in blah blah blah)
in py3k dict comprehensions work like this:
d = {k:v for k, v in iterable}
in py2k you can use fortran's suggestion.
Use python dict comprehensions: Here's the link to know more about it: Dict Comprehensions