Collections & Data Structures · Lesson 39

Create Dictionaries

A dictionary stores key-value mappings.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

Create Dictionaries

A dictionary stores key-value mappings. Keys must be hashable and unique; assigning an existing key replaces its value. Dictionaries are appropriate when values are retrieved by meaningful identifiers rather than by sequence position.

Learning goal: explain why Create Dictionaries behaves this way, apply it to a small example, and verify the result independently. Begin by being able to justify this first step: Create key-value pairs with a dictionary literal or dict constructor.

Deeper walkthrough

Read Create Dictionaries as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Create key-value pairs with a dictionary literal or dict constructor. Stage 2: Choose keys that are hashable and represent the lookup identity clearly. Stage 3: Read values by key, using get when a missing key should have controlled behaviour. Final checkpoint: Iterate over items when both key and value are needed, and verify expected keys before downstream use.

Mechanism

Follow the transformation

Create key-value pairs with a dictionary literal or dict constructor.

Choose keys that are hashable and represent the lookup identity clearly.

Read values by key, using get when a missing key should have controlled behaviour.

Evidence

Know what would convince you

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
Useful distinctionInput: Objects/values supplied to the operation.
How it works

Trace the mechanism step by step

  1. Create key-value pairs with a dictionary literal or dict constructor.
  2. Choose keys that are hashable and represent the lookup identity clearly.
  3. Read values by key, using get when a missing key should have controlled behaviour.
  4. Update/add keys deliberately and inspect whether nested mutable values are being shared.
  5. Iterate over items when both key and value are needed, and verify expected keys before downstream use.
Worked demonstration

Create Dictionaries

# Step 1 — Compute the right-hand expression and store its result in `record` for the next step.
record = {"id": 101, "region": "East"}
# Step 2 — Execute this statement and inspect how it changes the current value, object or program state.
record["sales"] = 25.5
# Step 3 — Display the current value explicitly so the result/state can be inspected during execution.
print(record["region"])
# Step 4 — Display the current value explicitly so the result/state can be inspected during execution.
print(record)
Expected / illustrative result
The key region retrieves East; adding sales extends the mapping without changing the existing keys.
Interpret the result.

For Create Dictionaries, trace the specific input through the mechanism above and independently verify one returned value, state change or side effect.

Distinctions & related ideas

Place the concept correctly

InputObjects/values supplied to the operation.
StateNames or mutable objects that may change during execution.
OutputReturned value, side effect, file, plot or exception to inspect.
Use deliberately

When it is appropriate

Use Create Dictionaries when it answers a defined question in Collections & Data Structures and its inputs/assumptions match the current data or program state.

Boundary conditions

When to stop or reconsider

Reconsider Create Dictionaries when the required information is unavailable, the operation would violate a validation/data boundary, or a simpler operation answers the question more transparently.

Common mistakes

Failure modes to recognise

  • Running the operation on the wrong object/type or in the wrong environment.
  • Inferring correctness from “no exception” without checking the produced value/state.
  • Hiding a boundary case instead of making its behaviour explicit.
Verification

How to check the result

  • Trace a tiny input by hand and compare the runtime result.
  • Inspect type, value/shape and any mutation/side effect explicitly.
  • Run an edge or invalid case and confirm the exception/behaviour is deliberate.
Hands-on practice

Demonstrate understanding

Try this:

Construct a tiny example of Create Dictionaries. First create key-value pairs with a dictionary literal or dict constructor. Then choose keys that are hashable and represent the lookup identity clearly. Predict the result before execution and explain one boundary or failure case.

Use the smallest values that expose the language rule. Write the expected value and type first, then compare the actual state/output with that prediction.
Knowledge check

Check reasoning, not memorisation

Which approach best demonstrates understanding of Create Dictionaries?

Quick reference

Remember the logic

Step 1Create key-value pairs with a dictionary literal or dict constructor.
Step 2Choose keys that are hashable and represent the lookup identity clearly.
Step 3Read values by key, using get when a missing key should have controlled behaviour.
Step 4Update/add keys deliberately and inspect whether nested mutable values are being shared.
Lesson summary

What to remember

  • A dictionary stores key-value mappings. Keys must be hashable and unique; assigning an existing key replaces its value. Dictionaries are appropriate when values are retrieved by meaningful identifiers rather than by sequence position.
  • Identify the Python objects and types involved.
  • Running the operation on the wrong object/type or in the wrong environment.
  • Trace a tiny input by hand and compare the runtime result.