score:1

like the error says, you can not iterate over a date object, so for start_date in seven_days will not work.

you can however use a for loop here like:

for dt in range(7):
    bookings.setdefault(today+datetime.timedelta(dt), 0)

a dictionary has a .setdefault(..) function that allows you to set a value, given the key does not yet exists in the dicionary. this is thus shorter and more efficient than first checking if the key exists yourself since python does not have to perform two lookups.

edit: since dictionaries are ordered in insertion order (in the cpython version of that was already the case, but seen as an "implementation detail"). since , you can thus sort the dictionaries with:

bookings = dict(sorted(bookings.items()))

prior to , you can use an ordereddict [python-doc]:

from collections import ordereddict

bookings = ordereddict(sorted(bookings.items()))

Related Query

More Query from same tag