Learn Whether to Use a Dictionary or a List in Python: Absolute Beginners Tutorial

Every Python program stores data, and the two structures you will reach for first are lists and dictionaries. Choosing between them is not about which one is "better" — it is about which one matches how your program needs to access, organize, and retrieve that data. This tutorial walks you through both structures from scratch, compares them side by side, and gives you a clear decision framework so you always pick the right one.

When you first start writing Python, storing data feels straightforward. You throw values into a list, loop through them, and move on. That approach works until you find yourself scanning hundreds of items to locate one specific value, or struggling to connect a piece of data to a meaningful label. That is the moment when understanding dictionaries becomes essential. The good news is that neither structure is complicated on its own. The challenge is knowing which situation calls for which tool — and that is exactly what this tutorial will teach you.

What Is a List in Python?

A list is an ordered collection of items stored in a specific sequence. Each item sits at a numbered position called an index, starting at 0 for the first element. You create a list using square brackets, and you can store any mix of data types inside it.

python
# Creating a list of grocery items
groceries = ["eggs", "milk", "bread", "butter"]

# Accessing items by index position
print(groceries[0])    # eggs
print(groceries[2])    # bread

# Adding a new item to the end
groceries.append("cheese")
print(groceries)       # ["eggs", "milk", "bread", "butter", "cheese"]

Lists are mutable, which means you can add, remove, and change items after creation. They support slicing (grabbing a range of items), sorting, and reversing. Because items are stored in order, you can loop through them and always get the same sequence back.

Note

Lists are ideal when position matters. If you need to know that "eggs" is the first item and "bread" is the third, a list preserves that ordering naturally through integer-based indexing.

Common list operations

python
scores = [88, 72, 95, 63, 81]

# Slicing: grab items 1 through 3
middle = scores[1:4]
print(middle)          # [72, 95, 63]

# Sorting in place
scores.sort()
print(scores)          # [63, 72, 81, 88, 95]

# Checking membership (scans every item)
print(95 in scores)    # True

# Removing by value
scores.remove(72)
print(scores)          # [63, 81, 88, 95]

Notice the membership check on line 9: Python has to walk through the entire list to find whether 95 exists. For a short list that is fine, but for thousands of items this linear scan becomes a performance concern. Keep that in mind as we move to dictionaries.

What Is a Dictionary in Python?

A dictionary stores data as key-value pairs inside curly braces. Instead of accessing items by a numbered index, you access them by a unique, descriptive key. Under the hood, Python uses a hash table to map each key directly to its value, which makes lookups very fast regardless of the dictionary's size.

python
# Creating a dictionary of student grades
grades = {
    "Alice": 92,
    "Bob": 85,
    "Clara": 78
}

# Accessing a value by its key
print(grades["Alice"])       # 92

# Adding a new key-value pair
grades["David"] = 90
print(grades)
# {"Alice": 92, "Bob": 85, "Clara": 78, "David": 90}

# Safe access with .get() to avoid KeyError
print(grades.get("Eve", 0)) # 0 (Eve is not in the dict)

Every key in a dictionary must be unique and immutable. Strings, numbers, and tuples all work as keys. Lists cannot serve as keys because they are mutable — Python cannot generate a stable hash from an object that might change.

Pro Tip

Use .get() instead of bracket notation when you are not certain a key exists. It prevents your program from crashing with a KeyError and lets you supply a sensible default value.

Since Python 3.7, dictionaries officially preserve insertion order. That means the first key-value pair you add will always appear first when you iterate. However, dictionaries are not sequences: they do not support integer-based indexing or slicing the way lists do.

code builder click a token to place it

Build the statement that creates a dictionary mapping "name" to "Alice" and "age" to 30:

your code will appear here...
"age": { 30 [ user } "Alice", = ] "name":

Real-World Examples: Lists vs Dictionaries

Seeing abstract definitions is one thing. Seeing real-world patterns makes the choice obvious. Here are two scenarios that illustrate when each structure shines.

Scenario 1: A playlist of songs (use a list)

python
# Order matters — play songs in sequence
playlist = ["Bohemian Rhapsody", "Stairway to Heaven", "Hotel California"]

# Play the next song
current = playlist[0]
print(f"Now playing: {current}")

# Shuffle and re-sort easily
import random
random.shuffle(playlist)
print(playlist)

A playlist is a sequence. The order defines the listening experience, and you might slice it, shuffle it, or pop the next track off the front. Lists handle all of this naturally.

Scenario 2: Application settings (use a dictionary)

python
# Each setting has a descriptive name (key) and a value
config = {
    "theme": "dark",
    "font_size": 14,
    "auto_save": True,
    "language": "en"
}

# Instantly retrieve a setting by name
print(config["theme"])         # dark

# Update a setting
config["font_size"] = 16

You would never say "give me the setting at index 2." You say "give me the font size." The key-value structure of a dictionary maps directly to how you think about configuration data.

Key Differences Between Lists and Dictionaries

Now that you have seen both structures in action, here is a direct comparison across the characteristics that matter when making your choice.

List
Square brackets: ["a", "b", "c"]
Dictionary
Curly braces with key-value pairs: {"a": 1, "b": 2}
List
By integer index: my_list[0]
Dictionary
By key: my_dict["name"]
List
O(1) by index, O(n) to search for a value
Dictionary
O(1) average for key lookup via hash table
List
Ordered sequence with full slicing and sorting support
Dictionary
Preserves insertion order (Python 3.7+), but no slicing or in-place sorting
List
Lower memory per element (stores only references in a contiguous array)
Dictionary
Higher memory per entry (stores keys, values, and hash table overhead)
List
Allows duplicate values at different index positions
Dictionary
Keys must be unique; values can be duplicated
spot the bug click the line that contains the bug

This code tries to create a dictionary and print a value, but one line has a mistake. Click the buggy line, then hit check.

1 user = {
2 ["name"]: "Alice",
3 "age": 30
4 }
5 print(user["name"])
The fix: Change ["name"] to "name" on line 2. Dictionary keys must be immutable types. A list like ["name"] is mutable and cannot be hashed, so Python raises a TypeError. The correct line is "name": "Alice",.

Performance at a glance

The performance differences become significant as your data grows. Here is a simplified view of the time complexity for common operations.

List
O(1)
Dictionary
O(1) average
List
O(n) — scans every element
Dictionary
O(1) for keys, O(n) for values
List
O(1) amortized
Dictionary
O(1) average
List
O(n) — shifts remaining elements
Dictionary
O(1) average
List
Fast
Dictionary
Fast (slightly more overhead)
Common Beginner Mistake

Using a list to check membership across large datasets is a common performance pitfall. If you find yourself writing if username in users_list and your list contains thousands of entries, convert it to a set or dictionary for O(1) lookups.

How to Choose Between a Dictionary and a List in Python

Run through these four steps whenever you are deciding which structure to use. By the end, the answer will be clear.

  1. Identify how you will access the data

    Ask yourself whether you need to retrieve items by a descriptive label (like a name, ID, or setting) or by their numeric position in a sequence. If you need label-based access, a dictionary is the better fit. If position matters, choose a list.

  2. Consider whether order, slicing, or sorting matters

    If you need to slice ranges of items (my_list[2:5]), sort elements in place with .sort(), or frequently iterate over items in a specific sequence, a list is the natural choice. Dictionaries preserve insertion order since Python 3.7 but do not support slicing or in-place sorting.

  3. Evaluate lookup frequency and data size

    If your program frequently searches for whether an item exists or retrieves a value by a known identifier, a dictionary provides O(1) average lookup time regardless of size. Lists require O(n) search time, which becomes noticeably slower as data grows. For large datasets with frequent lookups, dictionaries are the better performer.

  4. Check your key requirements

    Dictionary keys must be immutable (strings, numbers, or tuples). If you cannot define a unique, immutable key for each item, or if your data is simply a collection of similar values with no natural label, a list is the simpler and more appropriate structure.

Combining both structures

In practice, you will frequently use lists and dictionaries together. A common pattern is a list of dictionaries, where each dictionary represents one record.

python
# A list of dictionaries — each dict is one student record
students = [
    {"name": "Alice", "grade": 92},
    {"name": "Bob", "grade": 85},
    {"name": "Clara", "grade": 78}
]

# Access the second student's name
print(students[1]["name"])  # Bob

# Find all students with a grade above 80
honor_roll = [s for s in students if s["grade"] > 80]
print(honor_roll)
# [{"name": "Alice", "grade": 92}, {"name": "Bob", "grade": 85}]

The list preserves the order of students (maybe sorted by enrollment date), while each dictionary gives you labeled access to that student's individual fields. This combination gives you the strengths of both structures at once.

Python Learning Summary Points

  1. Use a list when position, order, slicing, or sorting are central to your data's purpose. Lists store items in a numbered sequence and are accessed by integer index.
  2. Use a dictionary when you need to map descriptive keys to values and want fast, constant-time lookups. Dictionaries use a hash table internally, giving O(1) average access time for key-based operations.
  3. Dictionaries preserve insertion order as of Python 3.7, but they are not sequences. They do not support slicing or in-place sorting the way lists do.
  4. Dictionaries use more memory per entry than lists because they store keys, values, and hash table overhead. If memory efficiency is critical and you do not need key-based access, prefer a list.
  5. In real-world Python code, lists and dictionaries are frequently combined (a list of dictionaries is one of the patterns you will encounter regularly) to get the benefits of both ordered sequencing and labeled data access.

The decision between a list and a dictionary comes down to one central question: do you access your data by position, or by a meaningful key? Answer that, and the right structure follows naturally. As you write more Python, you will develop an instinct for this choice — and reaching for the right tool will become second nature.

check your understanding question 1 of 5

Frequently Asked Questions

A list stores items in a numbered sequence accessed by integer index positions (0, 1, 2, etc.), while a dictionary stores items as key-value pairs accessed by unique, descriptive keys. Lists use square brackets [] and dictionaries use curly braces {}.

Use a dictionary when you need to look up values by a meaningful label (like a username, product ID, or setting name), when you need fast lookups regardless of data size, or when the relationship between a key and its value is the important part of your data. Dictionaries provide O(1) average lookup time compared to O(n) for searching through a list.

Use a list when the order and position of items matters, when you need to sort or slice data, when you are working with a simple collection of similar items (like a list of names or scores), or when you need to access elements by their numeric position in a sequence.

It depends on the operation. Dictionaries are faster for lookups, deletions, and membership checks because they use a hash table internally, giving O(1) average time complexity for these operations. Lists are faster for iteration and use less memory per element. Accessing a list element by its index is also O(1), but searching for a value in a list requires scanning each element and is O(n).

Yes. Starting with Python 3.7, dictionaries officially preserve the insertion order of their keys as part of the language specification. This means the first key-value pair you add will always appear first when you iterate over the dictionary. However, dictionaries are not sequences like lists: they have no concept of integer-based indexing or slicing.

Yes. Dictionary values can be any Python object, including lists, other dictionaries, tuples, or custom objects. For example, you could map a student name (key) to a list of their grades (value). However, dictionary keys must be immutable types like strings, numbers, or tuples. Lists cannot be used as dictionary keys.

In both cases you use the in keyword. For a list, x in my_list checks every element one by one (O(n) time). For a dictionary, x in my_dict checks only the keys using a hash table, which is O(1) on average. To check dictionary values, use x in my_dict.values(), which is O(n) since it scans all values.

Accessing a non-existent key with bracket notation (my_dict['missing_key']) raises a KeyError. To avoid this, use the .get() method, which returns None (or a default value you specify) when the key is not found. For example, my_dict.get('missing_key', 'default') returns 'default' instead of raising an error.

Yes, generally. Dictionaries store both keys and values along with an internal hash table for fast lookups, which requires more memory overhead per entry compared to lists. Lists only store references to their elements in a contiguous array. If memory is a primary concern and you do not need key-based access, a list is typically the more efficient choice.

You cannot sort a dictionary in place the way you can with a list using .sort(). However, you can create a new dictionary sorted by keys or values using the sorted() function. For example, dict(sorted(my_dict.items())) returns a new dictionary sorted by keys. Since Python 3.7, the resulting dictionary preserves the sorted insertion order.