7/13
Part 2 of 4 · Three moves you'll use everywhereLearn and do

Group with a dict of lists

Grouping puts each item into a list under its key. setdefault(key, []) hands you the list for that key, and makes an empty one the first time:

words = ["tent", "lamp", "tarp", "kettle", "lantern"]
by_letter = {}
for w in words:
    by_letter.setdefault(w[0], []).append(w)
print(by_letter)
{'t': ['tent', 'tarp'], 'l': ['lamp', 'lantern'], 'k': ['kettle']}

Each list keeps its items in the order they came. collections.defaultdict(list) does the same job with by_letter[w[0]].append(w).

In the editor, write by_day(orders): each day's order IDs, grouped and in order.