FreeLesson 1 of 36 min read

Lists and dictionaries in Python: what is the difference?

Learn how Python lists store items by position and dictionaries store values by key, and why that changes how fast you can find things.

Almost every program keeps a collection of things: names, scores, products, messages. How you store that collection decides how quickly the computer can find what you need. Python's two most common containers are the list and the dictionary. They look similar at first, but they answer different questions.

A list: items in order, found by position

A list keeps items in a row. Each item has a position number called an index, starting at 0. You ask a list, "What is at position 2?" and it answers immediately.

fruits = ["apple", "banana", "cherry"]
print(fruits[0])      # apple
fruits.append("date")  # add to the end
print(fruits)         # ['apple', 'banana', 'cherry', 'date']

Lists are great when order matters, when you want to loop through everything, or when the same value can appear more than once.

A dictionary: values found by key

A dictionary stores pairs. Each pair has a key and a value. Instead of asking "What is at position 2?", you ask "What belongs to this key?"

ages = {"Ana": 17, "Ben": 18}
print(ages["Ben"])   # 18
ages["Cara"] = 16     # add a new pair
print(ages)          # {'Ana': 17, 'Ben': 18, 'Cara': 16}

A few rules about dictionary keys:

  • Each key appears only once. Assigning to an existing key replaces its value.
  • Keys must be unchangeable types, such as strings, numbers or tuples. A list cannot be a key.
  • Values can be anything, including lists or other dictionaries.
  • Asking for a key that does not exist raises a KeyError.

Why dictionaries find things quickly

Suppose you want to know whether something is in the collection. With a list, Python has no shortcut: it checks the first item, then the second, then the third, until it finds a match or reaches the end. A list of a million items might need a million checks.

A dictionary uses a technique called hashing. Python turns the key into a number and uses that number to jump straight to the place where the value is stored. On average, the lookup takes about the same time whether the dictionary holds ten pairs or a million.

Example: a phone book, two ways

As a list of pairs:

book = [("Ana", "555-0101"), ("Ben", "555-0102"), ("Cara", "555-0103")]

To find Cara, the computer does this:

Step 1: look at ("Ana", ...). Not Cara.

Step 2: look at ("Ben", ...). Not Cara.

Step 3: look at ("Cara", ...). Found it.

As a dictionary:

book = {"Ana": "555-0101", "Ben": "555-0102", "Cara": "555-0103"}

book["Cara"]

Step 1: hash "Cara" to find its storage spot.

Step 2: read the value there: "555-0103".

With three people the difference is tiny. With thousands, the list keeps getting slower while the dictionary stays quick.

Choosing between them

Ask yourself what question you will ask most often:

  • "What is the 5th item?" or "Go through them in order": use a list.
  • "What is the value for this name or ID?": use a dictionary.
  • "Is this item in my collection?" with lots of data: a dictionary (or a set) is much faster.
  • Duplicates allowed and important: use a list.

Next time you start a program, pause before typing the first bracket. Decide whether you will look things up by position or by name, and pick the container that matches. In the next lesson we will see two ways of controlling the order in which items come out: stacks and queues.

Key takeaways

  • A list stores items in order and finds them by index.
  • A dictionary stores key-value pairs and finds values by key.
  • Checking membership is slow in a big list but fast in a dictionary because of hashing.

Check your understanding

Question 1 of 3

You need to look up a student's name using their unique student ID. Which structure fits best?

Choose an answer

Question 2 of 3

ages = {"Ana": 17, "Ben": 18}. What happens when Python runs print(ages["Zoe"])?

Choose an answer

Question 3 of 3

Why is "x" in big_list usually slower than "x" in big_dict?

Choose an answer

Written with AI help. Spotted a mistake? Tell us.