Python List vs Dict for look up table
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
For a lookup table in Python, a dict is usually the default choice because it maps arbitrary keys to values with fast average lookup time. A list is the better choice only when your keys are dense integer indexes and the positional structure is part of the problem.
What Problem Are You Solving
A lookup table means "given a key, return the matching value quickly." The main question is what kind of key you have.
If the key is an integer index such as 0, 1, 2, and the range is compact, a list can be perfect:
That is not just fast. It is also semantically clear because the data is naturally ordered by position.
If the keys are names, codes, sparse ids, or any non-contiguous values, a dict fits much better:
Trying to store those values in a list would either waste space or force awkward index math.
Performance Differences
Indexing a list by position is constant time:
Dictionary lookup by key is also constant time on average:
The real difference is not that one is always faster in some universal sense. It is that they optimize different lookup shapes.
To find a value in a list by content, Python may need to scan:
That membership check is linear in the list length. By contrast, key lookup in a dict is designed for direct access by key.
When a List Is the Better Lookup Table
Use a list when:
- keys are small integers
- the range is dense
- order matters naturally
- missing entries are rare or easy to represent
For example, mapping month numbers to names:
This is compact enough and makes sense because the keys are fixed integers from 1 to 12.
When a dict Is the Better Lookup Table
Use a dict when:
- keys are not simple dense indexes
- keys may be sparse, such as
1001,7004,990031 - you need readable key-value intent
- missing keys are expected
Example:
This is a much better fit than creating a list large enough to reach index 990031.
Memory and Maintainability
Lists can be memory-efficient when indexes are dense. They become wasteful when the index space is sparse because you need placeholders for missing positions.
dict stores hash-table metadata, so it has overhead per entry, but it saves enormous space when keys are sparse or irregular.
Maintainability also matters. A lookup table expressed as a dict often communicates intent more clearly:
That is easier to read and safer to extend than a list plus a separate rule for converting keys into indexes.
Common Pitfalls
The biggest mistake is using a list for sparse numeric keys. A list indexed by user ids or status codes usually wastes memory and obscures the meaning of the data.
Another common issue is forgetting that list membership and list searching are different from direct positional access. If you are scanning a list to find a matching key, you no longer have an efficient lookup table.
People also choose dict automatically even when the problem is really ordered positional data. For month numbers, weekdays, or lookup tables based on compact indexes, a list can be simpler and just as fast.
Finally, do not optimize based on vague folklore. Choose the structure whose key model matches the problem first, then measure if performance truly matters.
Summary
- Use a list when keys are dense integer positions and order is inherent.
- Use a
dictwhen keys are arbitrary, sparse, or descriptive. - Lists are good for positional lookup;
dictis good for key-based lookup. - Sparse numeric keys are usually a bad fit for lists.
- Pick the data structure that matches the shape of the lookup, not just the one that sounds faster.
Related reading
- Python memory usage of numpy arrays
- python numpy ValueError operands could not be broadcast together with shapes
- Python Sets vs Lists
- python swapped tuple to dict
- Python locale error unsupported locale setting
- Python Logging - Disable logging from imported modules
- Python threads and queue example
- Python update a key in dict if it doesn't exist

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.