What is the easiest way to create a dictionary from an iterable and assigning it some default value? I tried:
>>> x = dict(zip(range(0, 10), range(0)))
But that doesn’t work since range(0) is not an iterable as I thought it would not be (but I tried anyways!)
So how do I go about it? If I do:
>>> x = dict(zip(range(0, 10), 0))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: zip argument #2 must support iteration
This doesn’t work either. Any suggestions?
You need the
dict.fromkeysmethod, which does exactly what you want.From the docs:
So what you need is: