Week 6: Tables & Dictionaries
3549 words
18 min read
2026-09-12T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
Tabular records, fields, keys, dictionaries, constant-time lookup, and grouping/frequency counting with dictionaries. # Computational Thinking — Week 6: Tables & Dictionaries IITM’s CT Week 6 covers **Tables and Dictionaries**.

Computational Thinking — Week 6: Tables & Dictionaries
IITM’s CT Week 6 covers Tables and Dictionaries.
The syllabus only names the topics, so the detailed notes below expand those concepts in the style used throughout the course.
TL;DR
Week 5 dealt with a one-dimensional structure:
textList [10, 20, 30]
Week 6 introduces richer organization:
Table
textStudent | Marks | City A | 80 | Delhi B | 65 | Pune
A table organizes data into:
- rows
- columns
- records
- fields
Dictionary
text{ "A": 80, "B": 65 }
A dictionary stores:
The central Week 6 mental model is:
1. From lists to tables
A list stores a sequence:
text[72, 84, 91]
But real datasets often contain multiple attributes per item.
For example:
textName, Age, Marks
A table lets us represent such structured records.
2. Table
A table is a collection of rows and columns.
Example:
| Name | Age | Marks |
|---|---|---|
| A | 20 | 75 |
| B | 19 | 88 |
| C | 21 | 64 |
3. Row
A row usually represents one record/entity.
For example:
textA, 20, 75
is one student's record.
So:
4. Column
A column represents one attribute/field measured across records.
Examples:
- Name
- Age
- Marks
So:
This connects directly to Stats Week 1:
- rows = observations
- columns = variables
5. Cell
A single intersection of a row and column is a cell.
Example:
For student B:
textMarks = 88
That individual value occupies one cell.
6. Record
A record is all information associated with one entity.
Example:
text{Name: B, Age: 19, Marks: 88}
Conceptually, a row and a record are closely related.
7. Field
A field is one named component of a record.
For:
text{Name: B, Age: 19, Marks: 88}
fields are:
textName Age Marks
8. Iterating through a table
Suppose we want average marks.
Conceptually:
texttotal = 0 count = 0 for each row in table: total = total + row.Marks count = count + 1
Then:
This is simply Week 1 iteration applied to structured records.
9. Filtering rows
Suppose we want students with:
textMarks > 70
Pseudocode:
textfor each row: if row.Marks > 70: process row
Here the condition checks one field of the current row.
10. Multiple conditions
Suppose we want:
students older than 18 and marks above 70.
textif row.Age > 18 AND row.Marks > 70:
This combines tables with Week 2–3 Boolean logic.
11. Selecting columns
Sometimes we do not need the whole record.
Example:
From:
| Name | Age | Marks |
|---|---|---|
| A | 20 | 75 |
we may only need:
textName Marks
Conceptually, selecting columns extracts only relevant fields.
12. Updating a table
A table may be mutable.
Example:
textrow.Marks = row.Marks + 5
updates one field in a record.
As with list updates, later computation sees the new value.
13. Example update
Before:
| Name | Marks |
|---|---|
| A | 75 |
| B | 88 |
Suppose A gets bonus 5.
After:
| Name | Marks |
|---|---|
| A | 80 |
| B | 88 |
Only one cell changed.
14. Finding maximum in a table
Suppose we want highest marks.
textmax_marks = first_row.Marks for each remaining row: if row.Marks > max_marks: max_marks = row.Marks
Same max pattern as Week 2.
15. Finding the record associated with maximum
Sometimes you need not only the maximum value, but also the student who achieved it.
Then maintain:
textbest_marks best_name
or entire best row.
Conceptually:
textbest_row = first row for each row: if row.Marks > best_row.Marks: best_row = row
16. Why storing the whole row helps
Suppose max is:
If you only store:
textmax_marks = 95
you may lose information about:
- who scored 95
- their city
- their age
Storing the full row preserves the associated record.
17. Table ordering
Tables may or may not have an inherent meaningful row order.
That depends on the problem.
A list's order is central.
A table is often more about structured records than positional meaning.
18. Dictionary
A dictionary stores mappings:
Example:
textmarks = { "A": 75, "B": 88, "C": 64 }
Here:
"A"is a key75is its value
19. Key
A key identifies an entry.
Example:
textmarks["B"]
conceptually gives:
text88
The key tells us which stored value we want.
20. Value
A value is the information associated with a key.
Example:
text"B" → 88
So:
21. Why dictionaries are useful
Suppose you have:
textstudent_id → marks
Without a dictionary, you might scan an entire list to find one student's score.
With a dictionary, conceptually you can directly ask:
textmarks[id]
This makes key-based lookup natural.
22. Example
textpopulation = { "Delhi": 20, "Mumbai": 18, "Pune": 7 }
Then:
textpopulation["Mumbai"]
returns:
text18
Conceptually.
23. Keys are typically unique
A dictionary normally uses each key once.
Bad conceptual mapping:
text"A" → 50 "A" → 80
Which value would
"A" mean?So dictionary keys identify entries uniquely.
24. Values need not be unique
This is fine:
text"A" → 80 "B" → 80 "C" → 65
Different keys may map to the same value.
Uniqueness matters for keys, not necessarily values.
25. Dictionary lookup
Conceptually:
textD[key]
means:
return the value associated withkey.
Example:
textD = {"x": 4, "y": 9}
Then:
textD["x"]
gives:
26. Updating dictionary values
Suppose:
textcount["apple"] = 3
and another apple appears.
Update:
textcount["apple"] = count["apple"] + 1
Now:
textcount["apple"] = 4
This is exactly the accumulator pattern from Week 1, but indexed by key.
27. Frequency counting with dictionary
Suppose data:
textA, B, A, C, A, B
We want frequency of each value.
A dictionary can represent:
textA → 3 B → 2 C → 1
This is one of the most important dictionary patterns.
28. Frequency-count pseudocode
Conceptually:
textfreq = empty dictionary for each x: if x already exists in freq: freq[x] = freq[x] + 1 else: freq[x] = 1
After processing all items, each key stores its count.
29. Trace frequency counting
Input:
textA, B, A, C, A
Progress:
textA {A:1}
then:
textB {A:1, B:1}
then:
textA {A:2, B:1}
then:
textC {A:2, B:1, C:1}
then:
textA {A:3, B:1, C:1}
Final:
30. Dictionary as generalized counter
Earlier:
textcount = 0
tracked one category.
Now:
textfreq[key]
lets us maintain many counters simultaneously.
This is a major abstraction jump.
31. Example: word frequencies
Input:
textcat dog cat bird dog cat
Dictionary:
textcat → 3 dog → 2 bird → 1
This pattern appears constantly in data processing.
32. Example: scores by student
textscores = { "A": 72, "B": 91, "C": 68 }
This is different from a frequency dictionary.
The value can represent any associated information, not just counts.
33. Dictionary values can be complex
A value could itself be a record:
textstudents = { "A": {age:20, marks:80}, "B": {age:19, marks:75} }
So dictionaries can organize structured data hierarchically.
34. Table vs dictionary
A table naturally represents many records with the same fields.
Example:
| ID | Age | Marks |
|---|---|---|
| A | 20 | 80 |
| B | 19 | 75 |
A dictionary naturally represents lookup by a unique key:
textA → record B → record
They can represent related information in different forms.
35. Converting table to dictionary conceptually
Table:
| ID | Marks |
|---|---|
| A | 80 |
| B | 75 |
Dictionary:
text{ A: 80, B: 75 }
If
ID uniquely identifies each row, it can serve naturally as a dictionary key.36. Lookup vs scan
Suppose there are many student rows.
To find student
"S123" in a plain table/list, a simple algorithm may scan:textrow 1 row 2 row 3 ...
A dictionary is conceptually designed for direct key lookup.
This is the key structural advantage.
37. Membership test
Before accessing a dictionary key, sometimes ask:
textdoes key exist?
This avoids trying to retrieve a value for a missing key.
Conceptually:
textif key exists in D: ... else: ...
38. Missing-key problem
Suppose:
textD = {"A": 5}
and algorithm asks:
textD["B"]
But
"B" does not exist.Depending on the computational model, this may be invalid/error-like behavior.
So key existence matters.
39. Safe update pattern
For frequency counting:
textif x exists: freq[x] = freq[x] + 1 else: freq[x] = 1
The
else handles first occurrence.Common mistake: initialize new frequency to 0
Suppose first occurrence of
"A" is encountered.If you do:
textfreq["A"] = 0
and stop there, you have failed to count that occurrence.
Typically first occurrence should produce:
textfreq["A"] = 1
Common mistake: reset count each time
Wrong:
textfor each x: freq[x] = 1
This means repeated occurrences always overwrite with 1.
Correct logic increments an existing entry.
Common mistake: key vs value confusion
Given:
textD = {"A": 80}
"A" is not the value.It is the key.
80 is the value.Keep this distinction precise.
Common mistake: assume values are unique
Two students may both score:
That does not violate dictionary structure if their keys are distinct.
44. Iterating over dictionary entries
Conceptually, one may iterate over:
- keys
- values
- key-value pairs
Example:
textfor each key in D: process key and D[key]
45. Example: find key with largest value
Suppose:
textD = { A: 70, B: 92, C: 85 }
Goal:
find student with highest marks.
Conceptually:
textbest_key = first key for each key: if D[key] > D[best_key]: best_key = key
Final:
textB
with value:
46. Why store key, not only max value
If you store only:
textmax = 92
you know the score but not who achieved it.
Instead:
textbest_key = B
lets you recover:
textD[B] = 92
47. Dictionary and binning connection
Suppose bin labels are:
textlow medium high
Instead of separate variables:
textlow = 0 medium = 0 high = 0
you can conceptually use:
textbins["low"] bins["medium"] bins["high"]
A dictionary generalizes the bin-count pattern.
48. Example
Data:
text4, 12, 8, 21
Bins:
textlow → 0 medium → 0 high → 0
After processing:
textlow → 2 medium → 1 high → 1
Same result as Week 4, but organized as key-value state.
49. Grouping by key
Suppose records:
| City | Sales |
|---|---|
| A | 10 |
| B | 20 |
| A | 15 |
| B | 5 |
We want total sales per city.
Dictionary:
textA → 25 B → 25
This is a powerful pattern:
50. Group-sum pseudocode
Conceptually:
texttotals = empty dictionary for each row: city = row.City if city not in totals: totals[city] = 0 totals[city] = totals[city] + row.Sales
This combines:
- tables
- dictionaries
- iteration
- selection
- accumulation
51. Group-count pattern
To count number of records per category:
textcounts[key] = counts[key] + 1
Example:
| Department |
|---|
| CS |
| Math |
| CS |
| Physics |
| CS |
Result:
textCS → 3 Math → 1 Physics → 1
52. Group-average pattern
To find average per category, maintain two dictionaries:
textsum[key] count[key]
Then:
This is a very general data-analysis pattern.
53. Example
Records:
textA: 10 A: 20 B: 30
Maintain:
textsum[A] = 30 count[A] = 2 sum[B] = 30 count[B] = 1
Then:
54. Table filtering + dictionary aggregation
Suppose:
Count passing students per department.
For each row:
- check:
text
Marks >= 40 - if true:
text
pass_count[Department]++
This is a realistic multi-concept algorithm.
55. Example
| Dept | Marks |
|---|---|
| CS | 80 |
| Math | 35 |
| CS | 55 |
| Math | 60 |
Passing:
textCS → 2 Math → 1
56. Table as list of records
A useful mental model is:
textTable = list of rows
where each row itself contains named fields.
Conceptually:
text[ {Name:A, Marks:80}, {Name:B, Marks:70} ]
This helps connect Week 5 lists with Week 6 tables.
57. Dictionary as function-like mapping
A dictionary behaves conceptually like a finite mapping:
For example:
This resembles a mathematical function where each valid key maps to one value.
58. But dictionary is stored data
Unlike an abstract mathematical function, a dictionary is typically mutable state.
You may do:
textD[A] = 80
then later:
textD[A] = 90
The stored mapping changes.
59. Updating based on previous value
Suppose:
textbalance["A"] = 100
Then deposit 50:
textbalance["A"] = balance["A"] + 50
Result:
textbalance["A"] = 150
Same assignment semantics as ordinary variables.
60. Nested dictionaries/tables intuition
You may encounter structures conceptually like:
textD[city][year]
or:
textstudent[id].marks
Do not let notation distract you.
Resolve it layer by layer:
- locate outer key/record
- locate inner field/key
- read/update value
61. Trace example
Start:
textD = empty
Input:
textA, B, A, A, B
Algorithm:
textfor each x: if x not in D: D[x] = 0 D[x] = D[x] + 1
Trace:
| x | Dictionary after step |
|---|---|
| A | {A:1} |
| B | {A:1,B:1} |
| A | {A:2,B:1} |
| A | {A:3,B:1} |
| B | {A:3,B:2} |
Final:
62. Table trace example
Table:
| Name | Score |
|---|---|
| A | 60 |
| B | 90 |
| C | 75 |
Algorithm:
textbest = first row for each remaining row: if row.Score > best.Score: best = row
Trace:
Start:
textbest = A,60
B:
so:
textbest = B,90
C:
false.
Final:
Common mistake: modify the wrong row
When iterating:
textfor each row:
make sure update applies to the current intended record.
A table may contain many similar fields.
Trace both:
- current row
- field being changed
Common mistake: confuse row index with ID
Suppose:
| ID | Marks |
|---|---|
| 105 | 80 |
| 200 | 90 |
ID = 105 does not mean row position 105.It is a field value.
A row index and an identifier are different concepts.
Common mistake: assume table rows are dictionary keys
A table and a dictionary are different structures.
A table may contain duplicate values in a column.
A dictionary key must uniquely identify an entry.
Only use a column as a key when it is suitable as an identifier.
Common mistake: overwrite instead of accumulate
Suppose total sales per city.
Wrong:
texttotal[city] = sale
This keeps only the latest sale.
Correct:
texttotal[city] = total[city] + sale
after proper initialization.
Common mistake: aggregate before checking key existence
If:
texttotal[city]
has never been initialized, then:
texttotal[city] = total[city] + sale
may be invalid.
Initialize first:
textif city not in total: total[city] = 0
Common mistake: one counter for many categories
Suppose you need counts for:
textA B C
A single variable:
textcount
cannot tell you separate category counts unless handled in some other structure.
Dictionary:
textcount[A] count[B] count[C]
solves that cleanly.
Common mistake: key-value reversal
Given:
textname → marks
do not later assume:
textmarks → name
Mappings are directional.
Multiple names may share the same mark, so reverse lookup may not even be unique.
70. Table vs dictionary mental model
textTABLE │ ├── many rows ├── fixed fields/columns ├── iterate records └── good for datasets DICTIONARY │ ├── unique keys ├── key → value ├── direct conceptual lookup └── good for mappings / counts / aggregates
71. Week 6 pattern library
Frequency count
textfreq[x]++
Sum by group
textsum[group] += value
Count by group
textcount[group] += 1
Average by group
textsum[group] / count[group]
Lookup
textvalue = D[key]
Update
textD[key] = new_value
72. Week 5 → Week 6 connection
Week 5:
Week 6:
and:
So your data structures become richer:
textsingle variable ↓ list ↓ table / dictionary
73. Connection to Statistics
This week is also very close to real data analysis.
Statistics frequently starts with a table:
textrows = observations columns = variables
Then computational algorithms:
- filter rows
- group categories
- count frequencies
- compute averages
- locate extremes
Dictionaries provide a natural way to implement many of these operations.
74. What you should actually master
By the end of CT Week 6, you should be able to:
- Explain a table in terms of rows and columns.
- Distinguish records, fields and cells.
- Iterate through rows.
- Filter rows using field conditions.
- Update values in a table.
- Find max/min records based on a field.
- Explain a dictionary as a key-value mapping.
- Distinguish key from value.
- Perform conceptual dictionary lookup.
- Update an existing dictionary entry.
- Handle a missing/new key.
- Build a frequency dictionary.
- Count items by category.
- Sum values by category.
- Compute grouped averages using sum/count state.
- Understand why dictionary keys should uniquely identify entries.
- Trace dictionary state step-by-step.
If these are comfortable, Computational Thinking Week 6 is under control.
Quick Recall & Flashcards
Test your active recall before proceeding to practice problems. Format:
Question :: Answer.- Week 6 Diagnostic: In a dictionary / hash map, what is the expected time complexity to look up a value by its key? :: Answer: A — Dictionaries provide O(1) average-time lookup by key via hash-based indexing.
- Week 6 Diagnostic: Can a dictionary have duplicate keys? :: Answer: B — Dictionary keys are strictly unique. Reassigning to an existing key overwrites the previous value.
Practice & Self-Check
Attempt these diagnostic questions to verify your operational understanding.
1
Question 1
In a dictionary / hash map, what is the expected time complexity to look up a value by its key?
MCQ
3 Unit Assessment
A. O(1)
B. O(N)
C. O(N^2)
D. O(log N)
2
Question 2
Can a dictionary have duplicate keys?
MCQ
3 Unit Assessment
A. Yes, multiple identical keys are allowed with different values
B. No, keys must be unique; assigning to an existing key updates its value
C. Only if keys are integers
D. Yes, up to a maximum of 256 keys