Choosing the right container is one of the first decisions in Python, and it shapes how readable and fast your code becomes. The Printable Python Beginner Cheatsheet covers lists, tuples, dictionaries and sets on its second page. This guide explains the differences and when each one earns its place.

Lists: ordered and changeable

A list keeps items in order and lets you add, remove or replace them:

tasks = ["email", "report"]
tasks.append("invoice")
tasks.insert(0, "call client")
tasks.remove("email")
print(tasks)        # ['call client', 'report', 'invoice']
print(tasks[-1])    # invoice

Use a list when order matters and the contents will change, such as a queue of tasks or a series of readings.

Tuples: ordered and fixed

A tuple is an ordered sequence that cannot be changed after it is created. This makes tuples a good choice for values that belong together, such as coordinates or a record:

point = (4, 9)
x, y = point        # tuple unpacking
print(x + y)        # 13

Because tuples are immutable, they can also be used as dictionary keys, which lists cannot. Choose a tuple when the data is a fixed group rather than a collection you will edit.

Dictionaries: key-value lookup

A dictionary maps unique keys to values. Lookups by key are fast, which makes dictionaries the usual choice for counting, grouping or matching records:

stock = {"apples": 12, "pears": 0}
stock["plums"] = 5
print(stock.get("kiwis", 0))    # 0, safe default
for item, count in stock.items():
    print(item, count)

Since Python 3.7, dictionaries keep insertion order. Use .get() when a key might be missing, and .items() when you need both keys and values in a loop.

Sets: unique items and fast membership tests

A set holds unique items with no guaranteed order. Sets are useful for removing duplicates and for comparing groups:

a = {"python", "sql", "git"}
b = {"git", "docker"}
print(a.union(b))          # all four skills
print(a.intersection(b))   # {'git'}
print("sql" in a)          # True, checked quickly

Checking whether an item is in a set is typically much faster than checking a list, especially for large collections.

A quick decision guide

  1. Need order and will change items? Use a list.
  2. Need a fixed group, or a value used as a key? Use a tuple.
  3. Need to look things up by a label? Use a dictionary.
  4. Need unique items or fast membership checks? Use a set.

Common mistakes

  • Using a list where a set would prevent duplicates automatically.
  • Trying to change a tuple. Build a new tuple instead.
  • Assuming a set keeps your original order. It does not.

Keep the decision guide next to your keyboard, or use the Printable Python Beginner Cheatsheet as a reference while you practice. Related: the full Python beginner syntax guide.

Thinking about performance without becoming obsessed

Beginners often worry that a list is slow and a set is fast, and they try to optimise everything from the start. Performance matters, but the first priority is correctness and clarity. The practical rule is simple: when you need to check whether an item is present in a large collection, a set or a dictionary is usually much faster than a list, because looking up a key does not require scanning every item. For small collections, the difference is negligible, and a list is perfectly fine.

Understanding this trade-off early helps you read other people's code. When a colleague uses a set to remove duplicates, you will recognise the intention immediately, and you will know why that choice keeps the program efficient as the data grows.

Nested structures and where they go wrong

Real data often nests one structure inside another, such as a list of dictionaries representing records. This is extremely common and very useful, but it creates a hazard that catches many learners. Modifying a dictionary that you retrieved from a list changes the original record, because both names refer to the same object. If you want an independent copy, you must ask for one explicitly. Learning to think about references, rather than copies, is one of the most valuable skills in Python.

A realistic example: counting words

A word counter shows how the four structures work together. You split a sentence into a list of words, use a dictionary to count occurrences, and use a set to record unique words. The code is short, but it demonstrates a common pattern: a list holds the ordered input, a dictionary accumulates results by key, and a set answers questions about membership. Once you can explain why each structure appears, you understand the design rather than merely copying it.

Write the counter yourself before reading a solution. Even a rough first attempt teaches more than studying a finished version, because it exposes the exact points where your mental model of the data is incomplete.

Choosing the right structure in practice

When you start a new piece of code, ask three questions. Does the order of items matter? Will I need to change the contents? Will I look items up by a label or check membership often? The answers point toward a list, a tuple, a dictionary or a set. Most real programs use several of them together, and that is a sign of a healthy design rather than a confusing one.

Mutability and why it matters for real programs

Mutability decides whether a structure can change after creation. Lists, dictionaries and sets can change, which makes them flexible for collecting data as a program runs. Tuples cannot change, which makes them safe to use as fixed records and as keys in dictionaries. Choosing between a mutable and an immutable structure is therefore a design decision about who is allowed to change the data, not merely a matter of syntax.

When a function receives a list, it can modify the original list, which is sometimes desirable and sometimes a source of surprising bugs. Defining which functions may change which data is one of the habits that separates clean programs from tangled ones.

Converting between structures when a program changes shape

Programs often need to move data from one structure to another. A list of pairs can become a dictionary, a dictionary can yield its keys as a list, and a list with duplicates can become a set. Converting deliberately, and checking the result, prevents subtle errors such as losing order or silently dropping repeated entries. Being fluent in these conversions lets you choose the structure that suits each stage of a program rather than forcing one structure everywhere.