Python Fundamentals for Data Pipelines (Data Structures, Iterators, Generators)
Build memory-efficient pipeline code with the right structures, iterators, and generators for streaming records.
In plain words
Choose dict, tuple, dataclass, or typed record. Next, consume cursors and files iteratively. Finally, flush batches, checkpoint, and close resources.
Think of it like…
An iterator is a ticket dispenser: each pull receives the next item, whereas a list is a pallet holding every ticket at once.
Where you’ve seen it
def chunks(rows, size=10_000): batch=[]; ... yield batch; batch=[] bounds parsing memory, while a Counter retains only grouped keys for a small cardinality dimension.
Words you’ll hear
- Data Structures
- Lists, tuples, sets, dictionaries, and dataclasses used according to ordering, mutability, membership, and record-shape needs.
- Iterators
- An object with
__next__that yields values one at a time and advances state across the source. - Generators
- A function containing
yield; it suspends after each item and enables memory-bounded extraction and batch processing. - Operational boundary
list(source)on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass. Separate IO, transformation, and output; type record shapes, batch generators explicitly, close resources with context managers, and materialize only the small collection actually needed more than once.
Visual diagram
Theory & deep dive
Data Structures: the working mechanism
Pipeline Python should represent records with predictable structures and expose input as iterators or generators so work is bounded by one batch rather than the whole source.
Choose dict, tuple, dataclass, or typed record. Consume cursors and files iteratively, then keep functions deterministic and bounded; this exposes the state governed by Generators.
def chunks(rows, size=10_000): batch=[]; ... yield batch; batch=[] bounds parsing memory, while a Counter retains only grouped keys for a small cardinality dimension.
Python Fundamentals for Data Pipelines: failure mode and production response
list(source) on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass.
Separate IO, transformation, and output; type record shapes, batch generators explicitly, close resources with context managers, and materialize only the small collection actually needed more than once. Track Generator memory as "roughly one item or one explicitly sized batch", and make Batch size visible to the dataset owner.
How it works, step by step
Step 1 of 41. Model
Choose dict, tuple, dataclass, or typed record. Pipeline Python should represent records with predictable structures and expose input as iterators or generators so work is bounded by one batch rather than the whole source.
- Generator memory
- roughly one item or one explicitly sized batch
- Iterators
- An object with `__next__` that yields values one at a time and advances state across the source.
When to use it & trade-offs
What it is
Pipeline Python should represent records with predictable structures and expose input as iterators or generators so work is bounded by one batch rather than the whole source. def chunks(rows, size=10_000): batch=[]; ... yield batch; batch=[] bounds parsing memory, while a Counter retains only grouped keys for a small cardinality dimension.
Why it matters: list(source) on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass. Separate IO, transformation, and output; type record shapes, batch generators explicitly, close resources with context managers, and materialize only the small collection actually needed more than once.
When to reach for it
- Build memory-efficient pipeline code with the right structures, iterators, and generators for streaming records.
- Choose Data Structures because it provides this capability: Lists, tuples, sets, dictionaries, and dataclasses used according to ordering, mutability, membership, and record-shape needs.
- Keep evidence for Generator memory at "roughly one item or one explicitly sized batch" so the last production stage-Sink - flush batches, checkpoint, and close resources-remains testable.
When to avoid or be careful
list(source)on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass.- Do not conflate Iterators with Data Structures. The second concept contributes different behavior: An object with
__next__that yields values one at a time and advances state across the source. - Release only after this guardrail is in place: Separate IO, transformation, and output; type record shapes, batch generators explicitly, close resources with context managers, and materialize only the small collection actually needed more than once.
Data Structures / Iterators / Generators - roles, limits, and operating cost
Strengths: Lists, tuples, sets, dictionaries, and dataclasses used according to ordering, mutability, membership, and record-shape needs. Pipeline Python should represent records with predictable structures and expose input as iterators or generators so work is bounded by one batch rather than the whole source.
Watch out: list(source) on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass.
__next__ that yields values one at a time and advances state across the source.Strengths: An object with __next__ that yields values one at a time and advances state across the source. In this workflow it supports: Transform - keep functions deterministic and bounded.
Watch out: The operating cost is real: large enough for I/O efficiency without exhausting RAM.
yield; it suspends after each item and enables memory-bounded extraction and batch processing.Strengths: A function containing yield; it suspends after each item and enables memory-bounded extraction and batch processing. Its output can be checked with Checkpoint unit.
Watch out: Operating guardrail: Separate IO, transformation, and output; type record shapes, batch generators explicitly, close resources with context managers, and materialize only the small collection actually needed more than once.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Data Structures, and how would Generator memory reveal it?”
- “Why is Iterators distinct from this behavior: a function containing yield; it suspends after each item and enables memory-bounded extraction and batch processing?”
- Production clue: list(source) on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass.
- Design probe: Separate IO, transformation, and output; type record shapes, batch generators explicitly, close resources with context managers, and materialize only the small collection actually needed more than once.
Benchmarks & rules of thumb
- Generator memory
- roughly one item or one explicitly sized batch
- Batch size
- large enough for I/O efficiency without exhausting RAM
- Checkpoint unit
- a deterministic page, offset, or file that can be retried
Common mistakes & follow-ups
Common interview mistakes
list(source)on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass.- Reporting success from a job exit code without checking Generator memory and Batch size.
- Treating this example as complete without replay, rollback, or access boundaries:
def chunks(rows, size=10_000): batch=[]; ... yield batch; batch=[]bounds parsing memory, while aCounterretains only grouped keys for a small cardinality dimension.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: Lists, tuples, sets, dictionaries, and dataclasses used according to ordering, mutability, membership, and record-shape needs.
- What does the recovery plan do when this occurs?
list(source)on a large connector forces every row into memory, while a one-shot generator reused in two consumers becomes empty after the first pass. - When is Data Structures insufficient for Batch size, and what does adding Iterators cost in Generator memory?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Python Fundamentals for Data Pipelines (Data Structures, Iterators, Generators) with data engineer interview Q&A and hands-on practice.