Is there a way I can initialize dictionary values to 0 in python taking keys from a list?

Viewed 27588

I have a list to be used as keys for the dictionary and every value corresponding to the keys is to be initialized to 0.

1 Answers

You can do with dict.fromkeys

In [34]: dict.fromkeys(range(5),0)
Out[34]: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}
In [35]: dict.fromkeys(['a','b','c'],0)
Out[35]: {'a': 0, 'b': 0, 'c': 0}
Related