Python courseLesson 3 of 11
Python course · Lesson 3 of 11
Python Data Structures for Data Engineering Interviews
Choose between list, tuple, set, dict, deque and Counter by their operations and costs, with the patterns that come up in data engineering coding interviews.
On this page
Choosing the right built-in structure is often the whole answer to a coding question. Know what each one is for and what its common operations cost. In a pipeline the same choice decides whether a job over a million records takes a second or an hour, because a lookup inside a loop is multiplied by the number of records.
| Structure | Ordered | Mutable | Duplicates | Lookup by value | Typical use |
|---|---|---|---|---|---|
list |
Yes | Yes | Yes | O(n) scan | Ordered records, stacks, batches |
tuple |
Yes | No | Yes | O(n) scan | Fixed records, dict keys |
set |
No | Yes | No | O(1) average | Membership, deduplication |
dict |
Insertion order | Yes | Unique keys | O(1) average by key | Lookups, grouping, counting |
collections.deque |
Yes | Yes | Yes | O(n) scan | Queues: fast appends/pops at both ends |
heapq (on a list) |
Heap order only | Yes | Yes | O(n) scan | Smallest item, top-N, merging sorted inputs |
Counter / defaultdict |
Insertion order | Yes | Unique keys | O(1) average by key | Counting and grouping without key checks |
What each operation costs
These are CPython costs. “Average” for sets and dicts assumes a reasonable hash function; the worst case happens when many keys collide, which is rare for built-in types such as int, str and tuples of them. “Amortised” means an occasional operation is slow (a list resize) but the average over many calls is constant.
| Operation | list |
tuple |
set |
dict |
deque |
|---|---|---|---|---|---|
Index by position s[i] |
O(1) | O(1) | not supported | not supported | O(1) at the ends, O(n) towards the middle |
x in s |
O(n) | O(n) | O(1) avg, O(n) worst | O(1) avg by key, O(n) worst | O(n) |
| Append at the end | O(1) amortised | not supported | add O(1) avg |
d[k] = v O(1) avg |
O(1) |
| Insert or remove at the front | O(n) | not supported | n/a | n/a | O(1) (appendleft, popleft) |
| Remove last item | O(1) (pop()) |
not supported | n/a | popitem() O(1) |
O(1) |
| Remove by value or key | O(n) | not supported | O(1) avg | O(1) avg | O(n) |
| Iterate all items | O(n) | O(n) | O(n) | O(n) | O(n) |
| Sort | O(n log n) | via sorted() |
via sorted() |
via sorted() |
via sorted() |
| Copy | O(n) | O(n) | O(n) | O(n) | O(n) |
Set algebra costs depend on both sizes: s | t is O(len(s) + len(t)), and s & t is O(min(len(s), len(t))) on average because Python iterates over the smaller set and probes the larger one. s - t is O(len(s)).
heapq and Counter |
Cost |
|---|---|
heapq.heapify(list) |
O(n) |
heapq.heappush, heapq.heappop |
O(log n) |
Peek at the smallest item, heap[0] |
O(1) |
heapq.nlargest(k, items) / nsmallest |
O(n log k) |
heapq.merge(*sorted_inputs) |
O(n log m) for n items from m inputs, lazy |
Counter(items) |
O(n) |
Counter.most_common(k) |
O(n log k); without k it sorts everything, O(n log n) |
Membership: use a set
seen_ids = {101, 102, 103}
print(102 in seen_ids, 999 in seen_ids)
True False
Checking x in some_list scans the list, which gets slow inside a loop over a large input. Converting to a set first turns repeated membership checks from O(n) into O(1) on average. You can check the difference yourself rather than take it on trust:
import timeit
ids_list = list(range(20_000))
ids_set = set(ids_list)
probes = list(range(19_000, 21_000)) # half hit, half miss, mostly near the end of the list
t_list = timeit.timeit(lambda: [p in ids_list for p in probes], number=3)
t_set = timeit.timeit(lambda: [p in ids_set for p in probes], number=3)
assert t_set * 20 < t_list, (t_list, t_set) # the set wins by far more than 20x on any machine
print("set is faster:", t_set < t_list)
set is faster: True
The exact timings depend on the machine, so the example asserts a wide margin rather than printing numbers. On 2,000 probes against 20,000 ids the list does up to 40 million comparisons; the set does about 2,000 hash lookups.
Deduplicate while keeping order
events = ["b", "a", "b", "c", "a"]
print(list(dict.fromkeys(events)))
['b', 'a', 'c']
Dicts preserve insertion order (guaranteed since Python 3.7), so dict.fromkeys keeps the first occurrence of each value. list(set(events)) also deduplicates but loses order.
Real records are dicts, which are not hashable, so you deduplicate on a key instead. Two variants come up constantly: keep the first record per key, or keep the latest version per key (the in-memory version of a CDC merge).
rows = [
{"order_id": 1, "status": "created", "updated_at": "2026-10-01T09:00"},
{"order_id": 2, "status": "created", "updated_at": "2026-10-01T09:05"},
{"order_id": 1, "status": "shipped", "updated_at": "2026-10-02T14:00"},
{"order_id": 1, "status": "created", "updated_at": "2026-10-01T09:00"}, # replayed duplicate
]
# Keep the first row per key: a set of keys already emitted.
seen, first = set(), []
for r in rows:
if r["order_id"] not in seen:
seen.add(r["order_id"])
first.append(r)
# Keep the latest row per key: compare a timestamp, not arrival order.
latest = {}
for r in rows:
current = latest.get(r["order_id"])
if current is None or r["updated_at"] > current["updated_at"]:
latest[r["order_id"]] = r
assert [r["status"] for r in first] == ["created", "created"]
assert latest[1]["status"] == "shipped" # the late replay did not overwrite the newer row
print(sorted((k, v["status"]) for k, v in latest.items()))
[(1, 'shipped'), (2, 'created')]
Comparing updated_at instead of “last one wins” matters: the replayed duplicate arrives last, and a plain latest[key] = r would roll order 1 back to created. ISO-8601 strings in the same format and time zone sort correctly as text; mixed formats must be parsed first.
Lookups: a dict is an in-memory hash join
Enriching records against a small reference table is a join. Build a dict once (O(m)), then each lookup is O(1), so the whole job is O(n + m) instead of O(n × m) for a nested loop.
customers = [("c1", "IN"), ("c2", "GB")] # small dimension table
orders = [("o1", "c1", 500), ("o2", "c2", 120), ("o3", "c9", 75)]
country_by_id = dict(customers) # build once
enriched, unmatched = [], []
for order_id, customer_id, amount in orders:
country = country_by_id.get(customer_id) # .get: a missing key is data, not a crash
if country is None:
unmatched.append(order_id)
else:
enriched.append((order_id, country, amount))
assert unmatched == ["o3"]
print(enriched)
[('o1', 'IN', 500), ('o2', 'GB', 120)]
Collect unmatched keys instead of dropping them: an order for an unknown customer usually means the dimension load is late, and someone should know. Spark’s broadcast join is the same idea at cluster scale.
Grouping and counting
from collections import Counter, defaultdict
orders = [("asha", 50), ("ben", 20), ("asha", 70)]
totals = defaultdict(int)
for customer, amount in orders:
totals[customer] += amount
print(dict(totals))
print(Counter(customer for customer, _ in orders).most_common(1))
{'asha': 120, 'ben': 20}
[('asha', 2)]
These are the in-memory equivalents of GROUP BY with SUM and COUNT. defaultdict(list) collects the rows themselves per key, which is how you group before writing one file per partition:
from collections import defaultdict
events = [("2026-10-01", "a"), ("2026-10-02", "b"), ("2026-10-01", "c")]
by_day = defaultdict(list)
for day, event_id in events:
by_day[day].append(event_id)
assert by_day == {"2026-10-01": ["a", "c"], "2026-10-02": ["b"]}
print(dict(by_day))
{'2026-10-01': ['a', 'c'], '2026-10-02': ['b']}
Unlike itertools.groupby, this does not need the input sorted by key; it does need every group to fit in memory.
Top-N without sorting everything
Sorting a million rows to keep ten is O(n log n). heapq.nlargest keeps a heap of size k and is O(n log k), and it works on any iterable, including a generator over a file.
import heapq
sales = [("north", 150), ("south", 230), ("east", 90), ("west", 410), ("central", 230)]
top3 = heapq.nlargest(3, sales, key=lambda r: r[1])
assert top3 == [("west", 410), ("south", 230), ("central", 230)] # ties keep input order
print(top3)
[('west', 410), ('south', 230), ('central', 230)]
When the data arrives as a stream, keep a bounded min-heap yourself. The smallest of the current top k sits at heap[0]; a new value replaces it only if it is larger.
import heapq
def top_k_stream(values, k):
heap = []
for v in values:
if len(heap) < k:
heapq.heappush(heap, v)
elif v > heap[0]:
heapq.heapreplace(heap, v) # pop the smallest and push v in one O(log k) step
return sorted(heap, reverse=True)
assert top_k_stream(iter([5, 1, 9, 3, 7, 9, 2]), 3) == [9, 9, 7]
print(top_k_stream(range(1_000_000), 3))
[999999, 999998, 999997]
Memory is O(k) however long the stream is. For “top N per group”, keep one heap per key in a dict. heapq.merge is the related tool for combining already-sorted files (for example sorted daily extracts) lazily, without loading them.
Queues and sliding windows
from collections import deque
window = deque(maxlen=3)
for value in [1, 2, 3, 4, 5]:
window.append(value)
print(list(window), sum(window) / len(window))
[3, 4, 5] 4.0
deque(maxlen=n) drops old items automatically, which is handy for moving averages over the last n events. Pipelines more often need a time window (“requests in the last 60 seconds”), where the number of items varies. Append on the right, evict expired items from the left, and keep a running total so each step is O(1) amortised instead of re-summing the window:
from collections import deque
def rolling_sum(events, window_seconds):
"""events: (epoch_seconds, value) in time order. Yields (ts, sum over (ts - window, ts])."""
window, total = deque(), 0
for ts, value in events:
window.append((ts, value))
total += value
while window[0][0] <= ts - window_seconds: # evict from the left: O(1) per item
_, old = window.popleft()
total -= old
yield ts, total
events = [(0, 5), (10, 3), (59, 2), (60, 4), (75, 1)]
result = list(rolling_sum(events, 60))
assert result == [(0, 5), (10, 8), (59, 10), (60, 9), (75, 7)]
print(result)
[(0, 5), (10, 8), (59, 10), (60, 9), (75, 7)]
At ts=60 the event from ts=0 falls out (the window is the half-open interval from 0 to 60, excluding 0); at ts=75 the one from ts=10 goes too. Each event is appended once and popped once, so the whole stream is O(n). Using a list with pop(0) would make every eviction O(window size).
Tuples as composite keys
Tuples are immutable, and therefore hashable when their elements are, so they can be dict keys or set members:
daily = {("2026-10-01", "north"): 150, ("2026-10-01", "south"): 230}
print(daily[("2026-10-01", "south")])
230
This is how you deduplicate or aggregate on a multi-column natural key such as (order_id, line_no) or (day, region). A tuple that contains a list is not hashable: hash((1, [2])) raises TypeError.
Interview traps
Each of these is a short snippet interviewers like to show and ask “what does this print?”.
from collections import Counter, defaultdict
import heapq
# 1. [[]] * 3 repeats one inner list three times.
grid = [[]] * 3
grid[0].append("x")
assert grid == [["x"], ["x"], ["x"]]
grid = [[] for _ in range(3)] # three separate lists
grid[0].append("x")
assert grid == [["x"], [], []]
# 2. Reading a missing key from a defaultdict inserts it.
counts = defaultdict(int)
_ = counts["typo"]
assert "typo" in counts # use counts.get("typo", 0) to read without inserting
# 3. Counter arithmetic drops zero and negative counts; subtract() keeps them.
stock = Counter(apples=2, pears=1)
assert stock - Counter(apples=3) == Counter(pears=1)
stock.subtract(Counter(apples=3))
assert stock["apples"] == -1
# 4. Changing a dict's size while iterating over it raises.
d = {"a": 1, "b": 2}
try:
for k in d:
if d[k] == 1:
del d[k]
except RuntimeError as exc:
print("RuntimeError:", exc)
d = {k: v for k, v in {"a": 1, "b": 2}.items() if v != 1} # build a new dict instead
assert d == {"b": 2}
# 5. heapq is a min-heap, and equal priorities fall back to comparing the next tuple field.
heap = []
heapq.heappush(heap, (1, {"job": "a"}))
try:
heapq.heappush(heap, (1, {"job": "b"})) # compares the two dicts: TypeError
except TypeError as exc:
print("TypeError:", exc)
heap = []
for seq, (prio, job) in enumerate([(1, {"job": "a"}), (1, {"job": "b"})]):
heapq.heappush(heap, (prio, seq, job)) # a unique sequence number breaks ties
assert heapq.heappop(heap)[2] == {"job": "a"}
# 6. list.sort() sorts in place and returns None.
nums = [3, 1, 2]
assert nums.sort() is None and nums == [1, 2, 3]
# 7. A one-element tuple needs a comma.
assert type((5)) is int and type((5,)) is tuple
RuntimeError: dictionary changed size during iteration
TypeError: '<' not supported between instances of 'dict' and 'dict'
Two more that are about concepts rather than syntax:
- “Dict lookups are O(1)” is the average case. Say “O(1) on average, O(n) in the worst case when keys collide”, and know that string hashing is randomised per process (
PYTHONHASHSEED), which is why set iteration order of strings can change between runs. See why dict and set lookups are O(1). - Copying.
list(rows)orrows.copy()copies the outer list but shares the inner dicts, so changing a field in the “copy” changes the original. See shallow vs deep copy.
Common mistakes
- Membership checks against a list inside a loop (quadratic time).
- Using a list as a queue with
pop(0), which shifts every element. Usedeque.popleft(). - Trying to use a list or dict as a dict key or set member (they are not hashable). Use a tuple of the key fields.
- Assuming sets keep order, or that string set order is the same on the next run.
- Deduplicating “latest wins” by arrival order when replays and late events exist. Compare a version or timestamp.
- Sorting everything to get a top 10. Use
heapq.nlargestor a bounded heap. - Re-summing a sliding window on every event (O(window) per step) instead of keeping a running total.
- Using
itertools.groupbyon unsorted input and getting the same key in several groups; use adefaultdict(list)or sort first.
In an interview
Many “process these records” questions reduce to: group with a dict, deduplicate with a set or dict.fromkeys, count with Counter, take the top N with heapq, and stream with a deque. A good answer has three parts:
- Name the structure and why: “a dict keyed by
customer_id, because I need constant-time lookups while I stream the orders”. - State the cost, average and worst case, for time and memory: “O(n + m) time, O(m) memory for the dimension dict”.
- Say what breaks at scale: when the dict no longer fits in memory you sort and merge, push the join into the database, or use Spark.
Practise with list vs tuple vs set, dict and set complexity, shallow vs deep copy and the data-structure questions in the top 50 Python interview questions. For classic hashing problems, see arrays and hashing.
Key takeaway
Pick the structure by the operation you do most: sets for membership, dicts for lookup and grouping, heapq for top-N, deques for queues and windows, tuples for fixed records and keys. Then say its cost out loud, including the worst case.
Progress is saved in this browser only. No account needed.

