Python interview questionsQuestion 2 of 4
Python interview question · Question 2 of 4
List vs tuple vs set: when would you use each in a data pipeline?
Short answer
Use a list for an ordered, changeable sequence such as records you append to. Use a tuple for a fixed record that should not change, and when you need a hashable value such as a composite dictionary key. Use a set when you need fast membership checks or deduplication, because lookups are O(1) on average, accepting that a set has no order and no duplicates.
Detailed explanation
| list | tuple | set | |
|---|---|---|---|
| Ordered | Yes | Yes | No |
| Mutable | Yes | No | Yes |
| Duplicates | Allowed | Allowed | Removed |
| Hashable | No | Yes, if its items are | No (frozenset is) |
x in ... |
O(n) | O(n) | O(1) average |
Example in a pipeline
batch = [] # list: rows accumulate in order
batch.append(("2026-10-01", "north", 150)) # tuple: a fixed record
batch.append(("2026-10-01", "south", 230))
already_loaded = {("2026-10-01", "north")} # set of tuple keys
to_load = [r for r in batch if (r[0], r[1]) not in already_loaded]
print(to_load)
[('2026-10-01', 'south', 230)]
Common mistakes
- Checking membership against a large list inside a loop.
- Expecting a set to preserve order.
- Thinking a tuple is “always hashable”:
(1, [2])is not, because it contains a list.
Progress is saved in this browser only. No account needed.