What you are looking for is dict.fromkeys along with dict.update.
From the docs
Create a new dictionary with keys from seq and values set to value.
Example Usage
>>> d = {}
>>> d.update(dict.fromkeys(range(10), 1))
>>> d
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}
Note, you need an dict.update
along with dict.fromkeys
, so as to update the dictionary in-place
Instead, if you want to create a dictionary and assign use the notation
>>> d = dict.fromkeys(range(10), 1)
>>> d
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1}
Unfortunately, for in-place update, you need to create a throw-away dictionary before passing to the dict.update
method. A non-intuitive method is to leverage itertools
from itertools import izip, repeat
d.update(izip(xrange(10), repeat(1)))
The same idea can be extended to OrderedDict which is often used, as an alternate for OrderedSet
as standard library does not provide one.