SECTION 26 / 31

Keep bill IDs when a count alone loses useful detail

1 min read
Ahmed → [18,20,25,29]. Each ID can be joined to its original bill to recover the shop and item rows.
Ahmed → [18,20,25,29]. Each ID can be joined to its original bill to recover the shop and item rows.
REAL COURSE DATA · KEYS, COUNTS AND CONNECTIONS

Keep bill IDs, then join back to records

Follow the dictionary as its keys and values change. Predict each lookup or update before revealing the next step.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next key, whether it already exists, and the value that will be stored.

billsByCustomer = {}
billByID = {}
foreach bill in bills {
    customer = bill.Customer
    if (not isKey(billsByCustomer,customer)) {
        billsByCustomer[customer] = []
    }
    billsByCustomer[customer] = billsByCustomer[customer] ++ [bill.BillNo]
    billByID[bill.BillNo] = bill
}
foreach id in billsByCustomer["Ahmed"] {
    originalBill = billByID[id]
}

A count such as Ahmed→18 cannot tell us which shops supplied the food. Ahmed’s saved IDs are [18,20,25,29] in original bill-number order. IDs are references, not item counts. Use a billByID dictionary to retrieve each record directly rather than rescanning the entire pile for every ID.

All-bill lists include Bill 25, even though it has no Food row. Keep original labels Advaith and Mohith from the cards; the spoken transcripts also use spellings such as Adwaith and Mohit. The examples use the supplied-card labels consistently.

Try it yourself

Is length(billsByCustomer[Ahmed]) equal to Ahmed’s Food row count?

Show explained answer

No. It is 4 bills; the Food row count is 18.

Try it yourself

What information does saving bill IDs preserve?

Show explained answer

A way to retrieve original shops, categories and item rows for follow-up questions.

Try it yourself

Why not deduplicate identical customer–category rows automatically?

Show explained answer

Different bill rows can represent separate purchases. Deduplication changes the unit and loses occurrences.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑