Software Engineer (SDE)pythonintermediate3 interactive MCQs in app
itertools Module
groupby requires pre-sorted consecutive keysislice lazy generator slicingcombinations vs permutations vs product
Core Concept
`itertools` provides C-speed lazy building blocks: `islice(iter, n)` slices streams/generators in O(1) space without converting to a list; `chain.from_iterable(lists)` flattens nested iterables without copying; `product`, `permutations`, `combinations` generate clean search spaces; `groupby(sorted_rows, key=...)` groups **consecutive** matching keys!
Worked Example
`itertools.chain.from_iterable([[1, 2], [3]])` flattens lists lazily without allocating a combined copy, and `itertools.islice(gen, 5)` pulls only the first 5 stream elements!
Common Pitfall
calling `itertools.groupby(unsorted_items, key=...)` splits non-adjacent matches into separate groups—ALWAYS sort by the exact same `key` first (or use `defaultdict(list)` for unsorted O(n) grouping)!
Recommended Readings & Practice
Practice itertools Module with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.