Lists vs Tuples vs Sets vs Dictionnaries in Python
đˇ Introduction
If you're preparing for a Python coding interview, one of the first questions you'll face is:
"Wich data structure would you use for this problem, and why?"
Python offers four fundamental built-in data structures: * List - ordered, mutable sequence. * Tuple - ordered, immutable sequence. * Set - unordered, unique elements. * Dictionary - key-value mapping.
At first glance, they might feel interchangeable. But choosing the right one can mean the difference between an efficient solution and a slow, buggy one. In this article, we'll explore each of these, discuss time complexity, and highlight common pitfalls that often come up in interviews.
đˇ Lists (list
)
A list is Python's most versatile container. It's ordered, allows duplicates, and can be modified after creation.
# Example: A list of vegetables
vegetables = ["asparagus", "beetroot", "celery"]
# Accessing elements
print(vegetables[0]) # [ 'asparagus' ]
# Adding and removing
vegetables.append("tomato")
vegetables.remove("beetroot")
â When to use:
- When order matters.
- When you need to frequently add/remove at the end.
- for problems involving arrays or dynamic sequences.
âąī¸ Complexity (average case):
- Index access:
O(1)
- Append:
O(1)
amortized - Insert/remove middle:
O(n)
- Search:
O(n)
â ī¸ Common pitfalls:
- Beginners often use lists for membership testing, e.g.:
if "celery" in vegetables: # O(n)
...
This works but is inefficient compared to a set.
đˇ Tuples (tuple
)
A tuple is like a list, but immutable. Once created, it cannot be changed.
# Example: A tuple of coordinates
coordinates = (10, 20)
# Tuple unpacking
x, y = coordinates
print(x, y) # (10, 20)
â When to use:
- When data should not change (e.g., fixed coordinates, RGB colors).
- As dictionary keys (since tuples are hashable).
âąī¸ Complexity:
- Similar to lists for access (
O(1)
)
â ī¸ Common pitfalls:
- Trying to modify a tuple:
coordinates[0] = 30 # â TypeError
đˇ Sets (set
)
A set stores unordered, unique elements, it's optimized for membership tests.
numbers = {1, 2, 3}
# Adding and removing
numbers.add(4) # O(1)
numbers.remove(2) # O(1)
# Membership test
print(3 in numbers) # True, O(1)
â When to use:
- When you need uniqueness (e.g., removing duplicates).
- When fast membership tests are needed.
âąī¸ Complexity:
- Add/remove:
O(1)
average - Membership test:
O(1)
â ī¸ Common pitfalls:
- Sets are unordered, so no indexing:
print(numbers[0]) # â TypeError
- Elements must be hashable (lists cannot be added to sets).
đˇ Dictionaries (dict
)
A dictionary
maps keys to values. Keys must be unique and hashable.
capitals = {"France": "Paris", "Japan": "Tokyo"}
# lookup
print(capitals["France"]) # Paris
# Insert/update
capitals["Germany"] = "Berlin" # O(1)
â When to use:
- When you need a mapping between data.
- When fast lookups are required.
âąī¸ Complexity:
- Lookup:
O(1)
average - insert/update:
O(1)
average - Delete:
O(1)
average
â ī¸ Common pitfalls:
- Accessing a missing key raises
KeyError
. Use.get()
to avoid crashes:
print(capitals.get("Spain", "Not found")) # Not found
Structure | Ordered | Mutable | Duplicates | Typical Use Case | Lookup Complexity |
---|---|---|---|---|---|
List | â | â | â | Dynamic ordered collection | O(n) |
Tuple | â | â | â | Fixed, immutable data | O(n) |
Set | â | â | â | Unique values, membership tests | O(1) |
Dictionary | â * | â | Keys: â | Key-value mapping | O(1) |
- Dictionaries preserve insertion order since Python 3.7, but conceptually they are still unordered mapping.
đˇ Conclusion
In Python coding interviews, you'll often be asked not just to solve a problem, but also to justify your choice of data structure. * Use lists when order matters and data is dynamic. * Use tuples when you need fixed, immutable data. * Use sets for uniqueness and fast membership checks. * Use dicts for mapping keys to values with fast lookups.
Mastering these four built-in collections data structures will help you tackle 80% of interview problems, and knowing their complexities and pitfalls will make your answers stand out.
Sept. 18, 2025, 5:14 p.m.