Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.
{k: 0 for k in key_list}
dict.fromkeys([1, 2, 3], 0)
{1: 0, 2: 0, 3: 0}
You can do with dict.fromkeys
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}
Add a comment
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
{k: 0 for k in key_list}dict.fromkeys([1, 2, 3], 0)would result in{1: 0, 2: 0, 3: 0}