Python List vs Dictionary: Differences and When to Use Each
items[0]), while a dictionary stores values by a meaningful key (prices["pen"]); use a list for an ordered sequence of similar items and a dictionary to look values up by name.A list is like a numbered queue: the first item is at index 0, the second at index 1, and so on. A dictionary is like a phone book: you do not care where an entry is, you look it up by name.
Beginners often reach for two parallel lists (one of names, one of prices) when a single dictionary would be clearer and faster. The reverse mistake also happens: using a dictionary with keys 0, 1, 2 when a plain list is what you mean.
prices_list = ["pen", 10, "book", 250]
prices_dict = {"pen": 10, "book": 250}
print(prices_list[1])
print(prices_dict["book"])List vs dictionary comparison table
| Aspect | List | Dictionary |
|---|---|---|
| Syntax | [10, 20, 30] | {"a": 10, "b": 20} |
| Access by | Integer position: items[2] | Key: data["b"] |
| Stores | Single values | Key-value pairs |
| Duplicates | Values can repeat | Keys are unique; a repeated key overwrites the old value |
| Order | Ordered by position | Keeps insertion order (guaranteed since Python 3.7) |
| Lookup speed | Searching for a value is O(n) | Lookup by key is O(1) on average |
| Missing item | IndexError | KeyError (or a default with get()) |
| Loop gives | The values | The keys (use .items() for pairs) |
When to use a list
Use a list for a sequence where the position or order is the point.
- A collection of similar things: scores, temperatures, product names.
- Data you sort, slice or process in order. See list methods.
- Rows of records, as in a list of dictionaries.
If you find yourself searching a list for a value just to get related information, a dictionary is probably the better structure.
When to use a dictionary
Use a dictionary when each value has a natural label.
- A record with named fields:
student = {"name": "Asha", "marks": 88}. - Fast lookups by id, username or product code.
- Counting or grouping things, where the key is the category.
The dictionary methods such as get() and items() make these jobs short. Dictionary keys must be hashable, so numbers, strings and tuples work but lists do not.
Combining both: a list of dictionaries
Real data usually needs both. A list holds many records in order, and each record is a dictionary with named fields. This is the same shape you get from JSON and from most web APIs, covered in nested data.
Examples you can run
Press Run, then edit the code and run it again.
Look up by position vs by key
The list answers by index, the dictionary by name. Notice how the dictionary version explains itself.
marks_list = [88, 92, 75]
marks_dict = {"Asha": 88, "Ravi": 92, "Meena": 75}
print(marks_list[1])
print(marks_dict["Ravi"])
print(marks_dict.get("John", "absent"))Expected output92
92
absent
A list of dictionaries
Each student is a dictionary; the list keeps them in order so we can loop.
students = [
{"name": "Asha", "marks": 88},
{"name": "Ravi", "marks": 92},
]
for student in students:
print(student["name"], student["marks"])
print(len(students))Expected outputAsha 88
Ravi 92
2
Keys are unique
Repeating a key overwrites its earlier value, unlike a list where both values would stay.
scores = {"Asha": 70, "Asha": 95}
print(scores)
print([70, 95])Expected output{'Asha': 95}
[70, 95]
Common mistakes
Using an integer index on a dictionary
✗ Wrong
marks = {"Asha": 88, "Ravi": 92}
print(marks[0])✓ Right
marks = {"Asha": 88, "Ravi": 92}
print(marks["Asha"])A dictionary is looked up by key, not by position. Use the key, or list(marks.values())[0] if you really want the first value.
Searching a list to find a related value
✗ Wrong
names = ["Asha", "Ravi"]
marks = [88, 92]
print(marks[names.index("Ravi")])✓ Right
marks = {"Asha": 88, "Ravi": 92}
print(marks["Ravi"])Store the pairs together in one dictionary so a lookup is direct.
Python List vs Dictionary: Differences and When to Use Each: questions people ask
Is a dictionary faster than a list in Python?
For finding an item by key, yes: a dictionary lookup is O(1) on average, while searching a list is O(n). For appending or looping, they perform similarly.
Are Python dictionaries ordered?
Yes. Since Python 3.7 a dictionary remembers the order in which keys were inserted, but you still cannot access items by numeric position.
Can a dictionary replace a list?
Only when you give every value a meaningful key. If the data is just a sequence, a list is simpler and makes your intent clear.
Keep learning
Step-by-step lessons:
Python ListsPython DictionariesPython Nested Data Structures
Related:
Python List vs Tuple: Differences and When to Use EachPython List vs Set: Differences and When to Use EachPython Dictionary get() MethodPython Dictionary Methods
More in data types and structures:
Python List vs Tuple: Differences and When to Use EachPython List vs Set: Differences and When to Use EachPython Mutable vs Immutable Objects Explained See all →