Most Pythonic Way to Build Dictionary From Single List

Use the .fromkeys() class method:

dayDict = dict.fromkeys(weekList, 0)

It builds a dictionary using the elements from the first argument (a sequence) as the keys, and the second argument (which defaults to None) as the value for all entries.

By it’s very nature, this method will reuse the value for all keys; don’t pass it a mutable value such as a list or dict and expect it to create separate copies of that mutable value for each key. In that case, use a dict comprehension instead:

dayDict = {d: [] for d in weekList}

Leave a Comment