Python & pandas Essentials · Lesson 7

Lists and Dictionaries

Lists and Dictionaries concerns Python dictionarys and collection-oriented reasoning.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

What Lists and Dictionaries actually means

Lists and Dictionaries concerns Python dictionarys and collection-oriented reasoning. Collections organise multiple values so that code can retrieve, update, iterate, group or deduplicate data without creating a separate variable for every item.

Lists and Dictionaries matters because Python and pandas provide the programmable layer for repeatable analysis. Explicit values, selections and transformations make analytical logic inspectable and reusable instead of dependent on manual editing.

Deeper walkthrough

Read Lists and Dictionaries as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Choose the data structure based on access pattern: position, key, uniqueness or immutability. Stage 2: Populate the collection with small, inspectable values. Stage 3: Read or update elements with the operation appropriate to the structure. Final checkpoint: Check edge cases such as a missing key, an empty collection or duplicate values.

Mechanism

Follow the transformation

Choose the data structure based on access pattern: position, key, uniqueness or immutability.

Populate the collection with small, inspectable values.

Read or update elements with the operation appropriate to the structure.

Evidence

Know what would convince you

  • Compare row/column counts, dtypes and missing values before and after the operation.
  • Trace a few representative rows or one group manually from source values to result.
Useful distinctionlist: Ordered, mutable sequence; duplicates allowed.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Choose the data structure based on access pattern

Choose the data structure based on access pattern: position, key, uniqueness or immutability. For Lists and Dictionaries, identify the exact state before this stage, the operation or rule applied here, and the observable state afterwards so the mechanism remains inspectable.

State focus: identify exactly what changed at this stage and what observable evidence confirms that change.
How it works

Trace the mechanism step by step

  1. Choose the data structure based on access pattern: position, key, uniqueness or immutability.
  2. Populate the collection with small, inspectable values.
  3. Read or update elements with the operation appropriate to the structure.
  4. Iterate over items, keys/values or paired sequences when processing data.
  5. Check edge cases such as a missing key, an empty collection or duplicate values.
Worked demonstration

Make the concept concrete

Demonstration

Python example

# Step 1 — Compute the right-hand expression and store its result in `ratings` for the next step.
ratings = [5, 4, 5, 3]
# Step 2 — Compute the right-hand expression and store its result in `counts` for the next step.
counts = {}
# Step 3 — Iterate through the collection so the indented block is applied once for each item.
for r in ratings:
    # Step 4 — Compute the right-hand expression and store its result in `counts[r]` for the next step.
    counts[r] = counts.get(r, 0) + 1
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print("unique:", set(ratings))
# Step 6 — Display the current value explicitly so the result/state can be inspected during execution.
print("counts:", counts)
# Step 7 — Iterate through the collection so the indented block is applied once for each item.
for index, value in enumerate(ratings, start=1):
    # Step 8 — Display the current value explicitly so the result/state can be inspected during execution.
    print(index, value)
Expected / illustrative result
unique: {3, 4, 5}
counts: {5: 2, 4: 1, 3: 1}
1 5
2 4
3 5
4 3
Interpret the result.

For Lists and Dictionaries, trace representative source rows/columns into the result and reconcile row counts, dtypes, keys or missing values that the operation could change.

Distinctions & related ideas

Know what this is — and what it is not

listOrdered, mutable sequence; duplicates allowed.
tupleOrdered, immutable sequence; useful for fixed records or keys.
dictKey → value mapping with unique keys.
setUnordered collection of unique hashable values; useful for membership and set algebra.
Use deliberately

When it is appropriate

Use Lists and Dictionaries when the data are naturally tabular and row grain, column meaning, keys and dtypes can be stated explicitly.

Boundary conditions

When to stop or reconsider

Reconsider the operation if row identity/grain is unclear, join keys are not validated, chained transformations hide state, or the task is better expressed with a simpler table operation.

Common mistakes

Failure modes to recognise

  • Changing row grain or row count without noticing it.
  • Joining/grouping on keys whose uniqueness or missingness was never checked.
  • Interpreting a derived column or aggregation without reconciling it to source rows and units.
Verification

How to check the result

  • Compare row/column counts, dtypes and missing values before and after the operation.
  • Trace a few representative rows or one group manually from source values to result.
  • For joins/reshapes/grouping, verify key uniqueness/cardinality and reconcile totals where totals should be preserved.
Hands-on practice

Demonstrate understanding

Try this:

Build a tiny, inspectable example of Lists and Dictionaries. First choose the data structure based on access pattern: position, key, uniqueness or immutability. Then populate the collection with small, inspectable values. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.

Work with 4–8 rows that contain the exact key/category/missing-value pattern you want to understand. Trace one row or group all the way through.
Knowledge check

Check reasoning, not memorisation

Before trusting a result from Lists and Dictionaries, which check provides the strongest evidence that you understand and applied it correctly?

Quick reference

Keep the important distinctions visible

Step 1Choose the data structure based on access pattern: position, key, uniqueness or immutability.
Step 2Populate the collection with small, inspectable values.
Step 3Read or update elements with the operation appropriate to the structure.
Step 4Iterate over items, keys/values or paired sequences when processing data.
Lesson summary

What to remember

  • Lists and Dictionaries concerns Python dictionarys and collection-oriented reasoning. Collections organise multiple values so that code can retrieve, update, iterate, group or deduplicate data without creating a separate variable for every item.
  • Choose the data structure based on access pattern: position, key, uniqueness or immutability.
  • Changing row grain or row count without noticing it.
  • Compare row/column counts, dtypes and missing values before and after the operation.