Module 8 · Python Data Structures — topic 22 of 22
Choosing appropriate data structures
Decide between list, tuple, set and dictionary by asking how data will be accessed and changed. Compare their strengths, speed of common operations and typical combinations in real programs.
Learning objectives
- Choose a structure from the access pattern
- Compare operation speed for lists, sets and dicts
- Combine structures to model real problems
Definition
Decide between list, tuple, set and dictionary by asking how data will be accessed and changed. Compare their strengths, speed of common operations and typical combinations in real programs.
Introduction
Most problems can be solved with any collection, but one choice usually makes the code shorter, clearer and faster. The trick is to ask a few questions about how the data will be used before you choose.
Why it matters
Choosing well early prevents painful rewrites. A program that checks membership in a list of a million items may take seconds where a set takes microseconds, and a dictionary can replace whole loops of searching.
Detailed explanation
Ask: Does order matter? Will items be added or removed? Are duplicates allowed? How will I find an item: by position, by membership or by a key?
Speed of x in collection: list and tuple check items one by one, so the time grows with size; set and dict use hashing, so the time stays roughly constant.
Adding at the end of a list is fast; inserting or removing at the front of a long list is slow. Dictionaries and sets add and remove quickly anywhere.
Memory: tuples are slightly smaller than lists; sets and dicts use more memory in exchange for fast lookups.
Combining structures is normal: a dictionary of student records, each with a list of marks; a set of seen IDs alongside a list that keeps order.
When in doubt, start with the simplest structure that matches the access pattern, and measure if performance matters.
Syntax
- Match the structure to how you will access the data.
- Combine structures freely.
- Comments can record why a structure was chosen.
Code examples
Same question, different structures
True True True 22
Measuring membership speed
set faster: True
Exact times vary by computer, but the set lookup is consistently far faster for large collections.
More examples
Modelling a small library
borrowed Python Basics
waitlisted for Python Basics
unknown member
Borrowed: {'B1': 'Asha'}
Waitlist: {'B1': ['Ben', 'Chen']}Step-by-step explanation
- books is a dict for lookup by ID; each value is a fixed (title, author) tuple.
- members is a set because only membership matters.
- borrowed maps a book ID to the member who has it.
- waitlist is a dict of lists because order of waiting matters.
- borrow() uses each structure for what it does best.
Expected output
Running “Modelling a small library” prints exactly:
Practical applications
- Web frameworks store request headers in dictionaries and routes in lists.
- Recommendation systems use sets for fast “already seen” checks.
- Data pipelines hold rows as lists of dictionaries before saving them.
Best practices
- Ask how the data will be accessed before choosing.
- Combine structures to fit each part of a problem.
- Measure before optimising, but avoid obviously slow patterns.
Common mistakes
Searching a list for lookups
Avoid
Prefer
A dictionary lookup replaces the whole search loop.
Using a list for uniqueness checks on large data
Avoid
Prefer
Membership tests on a large list get slower as it grows.
Key takeaways
- Lists for ordered sequences, tuples for fixed records, sets for uniqueness, dicts for lookups.
- Set and dict membership is fast; list membership grows with size.
- Real programs combine several structures.
Try it yourself
- 1Choose and justify structures for a quiz app: questions, the user’s answers, unique topics covered and scores by topic.
- 2Rewrite a loop that searches a list of (code, name) pairs into a dictionary lookup.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
13 questions cover the “Python Data Structures” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?