easy +8 pts

Zip Longest Fill

Implement a generator that zips multiple iterables, filling missing values with a default.

Write a generator function `zip_longest_fill(*iterables, fill=None)` that behaves like `itertools.zip_longest` but with a simpler twist: it yields lists containing one element from each iterable at the same index, continuing until the longest iterable is exhausted. When an iterable runs out of items, use `fill` for the remaining positions. The function must be a generator, so calling it returns an iterator that can be consumed lazily. **Input:** Any number of iterables (lists, strings, tuples, etc.) and an optional keyword-only argument `fill` (default `None`). **Output:** An iterator of lists, where each list has length equal to the number of iterables passed. The number of lists is the length of the longest iterable. **IMPORTANT:** Do not use `itertools.zip_longest` or any other `itertools` functions. Implement the logic yourself using loops and iterators. **Function signature:** `def zip_longest_fill(*iterables, fill=None):` **Example:** ```python list(zip_longest_fill([1,2], ['a'])) # [[1,'a'], [2,None]] ```

Constraints

- Any number of iterables, including zero. - Each iterable is finite and should be treated as one-pass (e.g., generators). - Complexity: O(total elements across all iterables) time, O(number of iterables) auxiliary space per yielded list.

Example

>>> list(zip_longest_fill([1,2], ['a']))
[[1, 'a'], [2, None]]
>>> list(zip_longest_fill('ab', [1,2,3], fill='x'))
[['a', 1], ['b', 2], ['x', 3]]
>>> list(zip_longest_fill())
[]
>>> list(zip_longest_fill([1], [2], [3]))
[[1, 2, 3]]
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Convert each iterable to an iterator using `iter()`.
Loop while True and collect one element from each iterator, using `next()` with a sentinel to detect exhaustion.
Break when all iterators are exhausted.
Yield a list, not a tuple, for each round.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.