PYTHON & CODING · QUESTION 07
What is the difference between a list, tuple, set, and dictionary in Python?
On this page
A list is an ordered, mutable sequence that allows duplicates. A tuple is an ordered sequence that cannot be reassigned item by item. A set stores unique hashable values and is useful for membership and set operations. A dictionary maps unique hashable keys to values. Choose based on whether you need order, mutation, uniqueness, or key-based lookup.
LET'S MAKE IT SIMPLE
First, the idea in plain English
Python collections are containers with different rules. Use a list when you want an editable sequence, such as steps in a recipe. A tuple is useful for a fixed group of values. A set is designed around uniqueness, so adding an existing item does not create another copy. A dictionary associates a key with a value, like a name linked to a phone number. Knowing these differences makes code easier to read and helps choose operations that fit the task.
A USEFUL COMPARISON
Picture it this way
A list is like an ordered shopping list you can edit; a tuple is a fixed coordinate; a set is a guest list where each name appears once; and a dictionary is a contact book indexed by name. These examples describe common uses, but Python’s actual rules—especially hashability and mutability—determine what operations are valid.
FOLLOW THE JOURNEY
Choose a collection by its behavior
Compare ordering, mutability, uniqueness, and lookup style for Python’s core collections.
Dictionary insertion order is guaranteed in modern Python. A tuple may contain mutable objects; immutability applies to its item references, not deep contents.
A table compares list, tuple, set, and dictionary by order, mutability, duplicates, and main use.
How to read this visual
Dictionary insertion order is guaranteed in modern Python. A tuple may contain mutable objects; immutability applies to its item references, not deep contents.
UNDER THE HOOD
How it works, step by step
Need positional order?
Choose a list or tuple when sequence order matters.
Why it matters: They support indexed access and preserve item order.
Need to edit items?
Use a list for a collection that changes; choose a tuple for a fixed record-like grouping.
Why it matters: Mutability communicates how the data is intended to change.
Need unique values?
Use a set to deduplicate values or check membership.
Why it matters: Set operations express uniqueness and intersection directly.
Need named lookup?
Use a dictionary to map keys to values.
Why it matters: A key is more meaningful than remembering a numeric position.
OUTSIDE THE TEXTBOOK
A practical example
For a small inventory, store ordered shipment events in a list, a fixed latitude/longitude pair in a tuple, the set of distinct product IDs in a set, and quantities keyed by product ID in a dictionary. Choosing the shape that matches the operation makes intent visible.
GO A LITTLE DEEPER
Technical details that matter
- Lists and tuples are sequences, so indexing is positional. Lists are mutable; tuples do not support item assignment.
- Set elements and dictionary keys must be hashable. Mutable built-in lists are not valid keys; immutable strings and many tuples are.
- Dictionary key lookup and set membership are average-case O(1), though worst-case behavior and implementation details depend on hashing.
- A tuple containing a list cannot be used as a dictionary key because the nested list makes the tuple unhashable.
Compare the collection types
| Property | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
| Ordering | Keeps insertion order | Keeps position order | No ordering contract | Keeps insertion order |
| Can you change items? | Yes | No item reassignment | Yes: add/remove members | Yes: add/remove mappings |
| Duplicates? | Allowed | Allowed | Values are unique | Keys are unique |
| Best fit | Editable sequence | Fixed grouping | Membership / uniqueness | Key-to-value lookup |
scores = [8, 8, 10]\nfixed_point = (3, 5)\nunique_scores = set(scores)\nprofile = {"name": "Mina", "score": 10}\n\nprint(unique_scores) # {8, 10}\nprint(profile["name"]) # MinaThe list preserves repeated scores, the tuple groups a fixed pair, the set removes duplicate values, and the dictionary retrieves a value by a descriptive key. Set display order should not be relied on.
MAKE A GOOD CHOICE
When is this useful?
- List: ordered collection that changes.
- Tuple: fixed record-like grouping or a key when every contained value is hashable.
- Set: uniqueness and membership checks.
- Dictionary: key-to-value mapping.
WATCH OUT FOR
Common misunderstandings
- Assuming a tuple makes nested objects deeply immutable.
- Expecting a set to keep insertion order or duplicates.
- Using a mutable object as a dictionary key.
- Using a list when direct key-based lookup would express the data better.
REAL-WORLD TRADE-OFFS
Limitations and failure cases
- Sets are unordered for purposes of application logic; do not rely on display order.
- Dictionaries require unique keys, so a later assignment replaces the earlier value.
- A wrong collection choice can make updates or lookups awkward.
WHAT TO REMEMBER
Key takeaways
- 1List: ordered and mutable.
- 2Tuple: ordered and not item-assignable.
- 3Set: unique hashable elements.
- 4Dictionary: key-value mapping with unique hashable keys.
PRACTICE THE FOLLOW-UP
What might an interviewer ask next?
When can a tuple be a dictionary key?
When all its elements are hashable. A tuple containing a list is not hashable and cannot be used as a key.
What is the average complexity of set membership?
Average-case membership is O(1) with hashing, though worst-case behavior can be worse and depends on collisions.
Are dictionaries ordered?
Yes, insertion order is guaranteed by modern Python, but a dictionary is still primarily a key-to-value mapping rather than a positional sequence.
SAY IT OUT LOUD
How to explain this in an interview
“I choose among these based on the data shape. A list is an ordered collection I can change, while a tuple is an ordered grouping I do not reassign item by item. A set keeps unique hashable values and is useful for membership checks. A dictionary maps unique hashable keys to values. For example, I might keep events in a list and quantities by product ID in a dictionary.”
Connect this question to the roadmap topic it supports.