Create a dictionary with list comprehension
问题 I like the Python list comprehension syntax. Can it be used to create dictionaries too? For example, by iterating over pairs of keys and values: mydict = {(k,v) for (k,v) in blah blah blah} # doesn\'t work 回答1: From Python 2.7 and 3 onwards, you should just use the dict comprehension syntax: {key: value for (key, value) in iterable} In Python 2.6 and earlier, the dict built-in can receive an iterable of key/value pairs, so you can pass it a list comprehension or generator expression. For