|
| Home |
|---|
The core difference between a Python dictionary and a list is how data is accessed: a list stores values in an ordered sequence accessed by numeric index, while a dictionary stores values as key-value pairs accessed by key. Both are mutable and can hold mixed data types, but they're built for different jobs.
In this tutorial you will learn how each structure works, how their performance compares for lookups and searching, when to use one over the other, how they behave when nested together, and the mistakes beginners make most often.
A list is an ordered, mutable collection of elements. Items are accessed by their position, called an index, starting at 0. Lists are defined with square brackets.
numbers = [10, 20, 30, 40]
print(numbers)
print(numbers[1])
[10, 20, 30, 40]
20
The list preserves the order items were added in, and numbers[1] retrieves the second element — index 1 — since Python indexing starts at 0.
A dictionary is an unordered-by-access, mutable collection of key-value pairs. Each value is retrieved using its unique key rather than a numeric position. Dictionaries are defined with curly braces.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
print(student)
print(student["name"])
{'name': 'Alice', 'age': 21, 'course': 'Python'}
Alice
Each value is labeled with a key that describes what it represents, which makes dictionaries easier to read than a list when the data has meaning attached to it, such as a person's name or age.
| Feature | List | Dictionary |
|---|---|---|
| Structure | Ordered sequence of values | Key-value pairs |
| Access Method | Numeric index (0, 1, 2...) | Key |
| Syntax | Square brackets [] |
Curly braces {} |
| Duplicates | Allowed for values | Keys must be unique; values can repeat |
| Lookup by value | O(n) — scans the list | O(1) average — hash table lookup by key |
| Order | Preserves insertion order, position-based | Preserves insertion order, but accessed by key, not position |
| Typical use case | Ordered collections of similar items | Labeled or structured data |
The same information can be represented either way — the difference is how it's accessed afterward.
# Using a list
student_list = ["Alice", 21, "Python"]
print(student_list[0])
# Using a dictionary
student_dict = {
"name":"Alice",
"age":21,
"course":"Python"
}
print(student_dict["name"])
Alice
Alice
Both return "Alice", but the dictionary version is self-documenting — student_dict["name"] is clearer than student_list[0], which requires remembering that position 0 means "name."
This is one of the most practical differences between the two structures. Searching a list for a value checks each element one at a time, which gets slower as the list grows. Searching a dictionary by key uses a hash table, which stays fast regardless of size.
# Searching in a list — O(n)
numbers = [10,20,30,40]
print(30 in numbers)
# Searching in a dictionary — O(1) average
student = {"name":"Alice","age":21}
print("age" in student)
True
True
Both return True, but for large datasets, checking in on a dictionary stays roughly constant-time no matter how many keys it has, while checking in on a list gets slower as the list grows, since Python has to scan through it element by element.
Lists and dictionaries are frequently combined — a common real-world pattern is a list of dictionaries, used to represent multiple structured records.
students = [
{"name":"Alice","age":21},
{"name":"Bob","age":23}
]
for student in students:
print(student["name"])
Alice
Bob
This pattern — a list of dictionaries — is exactly how most APIs return collections of records, such as a list of users or a list of products, each represented as its own labeled dictionary.
student[0], which raises a KeyError unless 0 happens to be an actual key[] with dictionary braces {} when writing empty collections — {} creates an empty dictionary, not an empty setTypeError because keys must be hashableA list stores values in order and accesses them by numeric index. A dictionary stores values as key-value pairs and accesses them by key instead of position.
For checking whether a value exists or looking something up by a label, a dictionary is faster on large data, since it uses a hash table for near-constant-time lookups. For accessing an item at a known position, both are equally fast.
Yes. Dictionary values can repeat freely — only the keys must be unique.
No. Dictionary keys must be hashable, and lists are mutable, so they can't be used as keys. Use a tuple instead if you need a sequence as a key.
Use a list of dictionaries when each record doesn't need a unique identifier and you'll process them in order. Use a dictionary of dictionaries when each record has a natural unique key, like a user ID, that you'll look up directly.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net