Indika Rajapaksha
Indika Rajapaksha

Reputation: 1168

How to create a python dict with a default value from a list?

I have a list of names (say months) in a list. How can I create a dict with same value (say 0) without a comprehension if it is possible in some way?

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']

What need is:

months_values = {'Jan': 0, 'Feb': 0, 'Mar': 0, 'Apr': 0, 'May': 0, 'Jun': 0}

Upvotes: 1

Views: 88

Answers (2)

Prem Jogi
Prem Jogi

Reputation: 57

month = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
dict1=dict.fromkeys(months,0)
print(dict1)

Upvotes: 1

lvzxy
lvzxy

Reputation: 87

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
months_dict = dict.fromkeys(months,0)

Upvotes: 2

Related Questions