The simple way to do this is with a dictionary comprehension and the calendar
module.
import calendar
days = {name: i for i, name in enumerate(calendar.day_name)}
print(days)
output
{'Thursday': 3, 'Friday': 4, 'Tuesday': 1, 'Monday': 0, 'Wednesday': 2, 'Saturday': 5, 'Sunday': 6}
Python 2.6 and older do not have the dictionary comprehension, but you can pass a generator expression to the dict
constructor.
days = dict((name, i) for i, name in enumerate(calendar.day_name))
It's also possible to make this dict
with datetime
, if you really want to. Eg,
from datetime import datetime, timedelta
oneday = timedelta(1)
day = datetime.today()
days = {}
for _ in range(7):
days[day.strftime('%A')] = day.weekday()
day += oneday
but using calendar is simpler & more efficient.