Why is itertools.tee dangerous on long or expensive iterators?
PICTURE THIS: HOW TO EXPLAIN IT
Simple meaning
tee buffers items that one branch has consumed but the other has not, which can grow to the full stream.
WHY — itertools instead of guessing?
Why interviewers care about itertools:
on itertools.
the situation, the default choice, and one exception - that reads as experience.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1tee buffers items that
one branch has consumed but the other has not, which can grow to the full stream.
- 2If both branches run
in lockstep the buffer stays small
- 3if one runs far
ahead you effectively stored a list.
- 4Prefer re-creating the iterator
or materializing once consciously.
- 5Common mistake
What juniors usually get wrong.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
tee buffers items that one branch has consumed but the other has not, which can grow to the full stream. If both branches run in lockstep the buffer stays small