COMP90059 Chap.8 Dictionaries, Sets and 2D Data
Dictionaries, Sets and 2D Data
Collection choice follows required behaviour. Lists preserve order and allow indexed change, tuples represent stable records, sets provide distinct membership and algebra, and dictionaries map unique keys to values. A set removes duplicates but does not preserve the original order contract, so ordered deduplication uses a set for seen membership plus a result list for first encounters.
Dictionary counting maintains a frequency invariant and can handle first occurrences with get or a default dictionary. Two-dimensional data stored as a list of row lists requires two lookups: row first, then column. Nested loops mirror that structure, while aliases between rows can cause one cell update to appear in several rows if construction reused the same inner list.
What this chapter covers
- 01
Sets store distinct values without positional indexing
- 02
Union, intersection and difference compare memberships
- 03
Set difference is directional rather than commutative
- 04
Dictionaries map unique keys to values
- 05
Get can provide a default for expected absence
- 06
Frequency tables update one key per observed item
- 07
Two-dimensional lists use row then column indexing
- 08
Independent rows must be constructed as separate list objects
- 09
Dictionary iteration order is not a substitute for an explicit sort rule
- 10
Hashable keys require stable equality and hash behaviour
- 11
Sparse records can omit fields whose absence has defined meaning
- 12
Grid traversals should derive bounds from the actual row structures
Worked example · free
Count labels with a dictionary
- 1Initialise an empty dictionary before the loop.
- 1For each label, read its current count with get using zero as the default.
- 1Increment and store the count for that label.
- 1The final mapping is red 3, blue 2 and green 1.
- 1Compare keys by their associated count to select red.
Key terms
- Set
- An unordered collection of distinct hashable values designed for membership and set algebra.
- Dictionary
- A mutable mapping that associates each unique hashable key with one value.
- Set union
- The set containing every value found in either of two input sets.
- Set intersection
- The set containing only values shared by both input sets.
- Frequency table
- A mapping from observed items to the number of times each item occurs.
- Two-dimensional list
- An outer list whose items are row lists, accessed by row index and then column index.
- Hashable key
- A value with stable hashing and equality behaviour that can safely identify an entry in a dictionary or an element in a set.
- Sparse mapping
- A dictionary that stores only present or exceptional entries while a documented default represents all omitted keys.
- Rectangular grid
- A two-dimensional structure whose rows share one column count, allowing a common column bound during nested traversal.
Dictionaries, Sets and 2D Data FAQ
Why not use only a set for ordered deduplication?
A set answers whether a value has appeared but does not by itself express the required first-seen sequence. A separate result list records that order while the set supports fast membership checks.
When should a missing key raise an error?
Use strict bracket lookup when the key is required and absence signals broken data. Use get when absence is expected and has a meaningful default that cannot be confused with a stored value.
How do I report every tied maximum?
Find the maximum count first, then collect every key whose count equals it. A single max call returns only one key and therefore cannot represent the full tie set.
Why did changing one grid row change several rows?
The grid was probably built with repeated references to the same inner list. Construct each row as a separate object, then verify identity before relying on independent cell updates.
How should I traverse a grid with uneven rows?
Treat each row as its own sequence and derive the inner bound from that row, not from the first row or a fixed width. Decide separately whether uneven shape is valid input or should be rejected during validation.
When is a dictionary better than parallel lists?
Use a dictionary when one stable key identifies each record and lookup by that key is central. Parallel lists require matching positions to remain synchronised across every update. If duplicate keys, ordering or multiple values per key matter, state those rules first and choose a mapping to lists or a list of records accordingly.
Exam move
Create a collection decision table with order, mutability, uniqueness, indexing and key lookup as columns. For one dataset, represent it as a list, tuple records, set and dictionary, then name the operation each version makes easier. Implement first-seen deduplication with a seen set and result list. Build a frequency table twice, once with get and once with a default dictionary, and state the invariant.
Construct a small grid with independent rows, flip one cell and trace row-column access. Finally, intentionally construct a grid with aliased rows and explain the unexpected mutation through an object diagram.
Working through Dictionaries, Sets and 2D Data in COMP90059? Sia is AskSia’s AI Computer Science tutor — ask any COMP90059 Dictionaries, Sets and 2D Data question and get a clear, step-by-step explanation grounded in how COMP90059 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.