I found the following stack overflow post about dict comprehensions in Python2.7
and Python 3+
: Create a dictionary with list comprehension in Python stating that I can apply dictionary comprehensions like this:
d = {key: value for (key, value) in sequence}
I tried it in Python 3. However, it raises an exception.
d = {'a':1, 'b':2, 'c':3, 'd':4}
{key : value for (key, value) in d}
{key : value for key, value in d}
Both versions raise a ValueError
saying that ValueError: need more than 1 value to unpack
.
What is the easiest / the most direct way to make a dictionary comprehension in Python3?