Dictionaries, frequencies and customer patterns
Learn keys and values through shared birthdays, word frequencies, shopping profiles and pronoun matching, with easy-English examples and interactive traces. Reuse course datasets and choose keys and values that answer the next question.
Study sections
Follow the order or choose a sectionA dictionary connects a key to a value
A dictionary is a collection of labelled entries. Each entry has a KEY, which tells us what to look for, and a VALUE, which is the info…
02Create the dictionary before assigning entries
Start with d = {}. This declares an empty dictionary. Then d[7] = 76 creates a key 7 and stores 76. d[21] = 74 creates a second entry.…
03A key is a label, not a list position
The dictionary {7:76,21:74} has two entries. Yet marks[21] is a valid lookup because 21 is a key. It does not request the 21st element.…
04Check whether the key exists before reading it
With d={7:0}, isKey(d,7) is True and d[7] is 0. isKey(d,99) is False. Do not assume a missing lookup automatically returns 0 or False i…
05Loop through keys, then read their values
Do not assume dictionary keys come out in the order they were inserted, in number order, or in alphabetical order. Our visual table cho…
06Build a marks dictionary and compute an average
Create chemMarks={}, then scan each score card. Store chemMarks[X.CardNo]=X.Chemistry. The lecture shows names as keys; the lab uses un…
07Create on the first visit; accumulate on later visits
For Kohli’s first illustrative 20, create runs[Kohli]=20. For a later 30, read 20 and replace it with 20+30=50. Writing runs[Kohli]=30…
08Use seen to avoid reversed pairs without marking cards
Mark the OUTER card before checking inner cards. The mark excludes the card itself and all earlier outer cards. A later inner card rema…
09Build a word-length frequency dictionary
Use the provided LetterCount field. The Word field retains punctuation, so counting the visible characters of “morning.” is a different…
10Count students from each city
Now use CityTown as the key and a student count as the value. Each record increments exactly one city. This is the same create-or-incre…
11Use day and month as a birthday key
Earlier weeks used all pairs, month bins and sorting. A dictionary provides another route: read each student once, compute a day-and-mo…
12Keep duplicate dates in a second dictionary
birthdays remembers every observed birthday; duplicates remembers only dates seen at least twice. Both are dictionaries of True flags.…
13Store the IDs who share each birthday
On Aditya’s card, birthdays[15 Mar] becomes [7]. On Monika’s card it becomes [7,21]. Here length(birthdays[15 Mar]) is 2, while length(…
14A dictionary value can be another dictionary
birthdays[15 Mar] is now {7:True,21:True}. To find IDs, use keys(birthdays[15 Mar]). To ask whether ID 21 is in that date group, test i…
15Choose what to remember and what work remains
Under the course’s constant-time dictionary-operation assumption, a birthday presence/count scan does one small amount of dictionary wo…
16First choose the unit: item rows, quantities or money
A bill item row represents one listed product entry. The quantity column says how much of that product was purchased. Price × quantity…
17Make the broad-category map explicit
Use the same rule for every bill. Keep the original detailed label alongside the broad group so the aggregation is reversible by return…
18Build customer → category → row counts
The lab pre-fills all five category keys with zero for a readable table. The pseudocode above creates only observed category keys. They…
19Separate simple groups before comparing detailed profiles
The lecture explores coarse bins before examining all profile differences. First set aside customers whose only observed category is Fo…
20Add absolute category gaps to measure distance
abs means absolute value: abs(8−13)=5 and abs(13−8)=5. Every gap is non-negative, so a larger Food count cannot cancel a smaller Utilit…
21Equal totals can hide different baskets
Abhinav totals 14 rows and Srivatsan totals 21. The absolute difference of totals is 7, but their five-category Manhattan distance is 1…
22Use a customer-by-customer table for all 15 pairs
Use indices only as local labels: 1 Abhinav, 2 Akshaya, 3 Ahmed, 4 Srivatsan, 5 Advaith, 6 Vignesh. These are not bill numbers or perma…
23Close pairs do not automatically form one close group
With close defined as distance ≤10, the qualifying pairs are 1–2 (7), 1–5 (10) and 5–6 (10). But the candidate group {1,5,6} contains a…
24Count food rows for each customer, then find the maximum
The source’s foodD creates a customer key only when a Food row occurs. A different valid representation creates every observed customer…
25Change the key to compare shops
To ask which shop has most Food rows, keep the same qualifying item test but use bill.Store as the key. The fixed three-shop problem co…
26Keep bill IDs when a count alone loses useful detail
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 orde…
27Turn “usually buys there” into a precise check
A vague statement such as “top food buyers shop at the top food shop” needs a rule. We test one explicit all-bills version: for the sel…
28Index noun and pronoun positions with lists
The dataset field is SerialNumber, beginning at 0. The slides use SerialNo for the same position role. Our lab maps the concept to the…
29Map each pronoun to the nearest earlier noun
For each pronoun p, reset matched to −1. Walk the sorted noun positions. Every n<p is a candidate; updating matched as n increases leav…
30Debug the first occurrence, the repeated key and empty input
Trace tiny inputs before processing the whole dataset. Use no records, one record, the same group twice, a missing key, a stored zero,…
31Choose keys and values from the question
A list preserves an ordered sequence and repeated occurrences. A dictionary connects unique keys to current values. Combining them give…
