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 python-3.7 dictionaries are ordered in insertion order (in the CPython version of python-3.6 that was already the case, but seen as an “implementation detail“). Since python-3.7, you can thus sort the dictionaries with:
bookings = dict(sorted(bookings.items()))
Prior to python-3.7, you can use an OrderedDict
[Python-doc]:
from collections import OrderedDict
bookings = OrderedDict(sorted(bookings.items()))
- [Chartjs]-Hide points in ChartJS LineGraph
- [Chartjs]-Using Chart.js onClick function, can you execute code from an undefined or null result?
Source:stackexchange.com