Lesson 5 of 12
Structured learning draftOrganise records with lists and dictionaries
A stock record needs a name and quantity, and the report needs several records. Combine dictionaries and a list to keep each item's fields together without relying on two separate lists that might drift out of alignment.
Learning objectives
- Represent several records in a list.
- Look up fields by dictionary key.
- Distinguish a required field from an optional one.
Organise records with lists and dictionaries: the working sequence
Trace the three stages of organise records with lists and dictionaries and compare them with the example.
The list contains complete item records rather than unrelated columns.
Each dictionary maps name and quantity to their values.
The loop retrieves both fields from the same record before printing it.
The list preserves the sequence of records. Each dictionary associates a field name with its value. Use direct lookup for required fields; a missing required key should be noticed rather than silently replaced.
stock = [
{'name': 'Pens', 'quantity': 12},
{'name': 'Books', 'quantity': 3},
]
for item in stock:
print(f"{item['name']}: {item['quantity']}")Pens: 12
Books: 3Try it, then change it
- Add a third item and check its output position.
- Rename one quantity key and read the resulting KeyError.
- Restore the field and add a genuinely optional note with item.get('note', '').
Common mistakes
- Keeping related fields in separate lists with different lengths.
- Using an optional-field default to hide missing required data.
- Repeating a dictionary key and expecting to retain both values.
Practice activity
Apply the lesson
Represent three library books with title and loan count. Print one line per record and explain how you detect a missing required title. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
What happens when a dictionary literal repeats the same key?
Lesson summary
- Keep an item's related fields in one record.
- Use field names to make access understandable.
- Required and optional fields need different handling.
Sources and further reading
- Data structures: dictionariesPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.