easy +8 pts

Yield from delegation

Build a generator that delegates to multiple nested iterables using `yield from`.

Write a generator function `flatten_deep(*iterables)` that yields all elements from the given iterables in order, flattening any nested iterables (lists, tuples, sets, etc.) completely. For example, `flatten_deep([1, [2, (3, 4)], 5], (6, [7, 8]))` should yield `1, 2, 3, 4, 5, 6, 7, 8` in that order. Strings are considered atomic and should be yielded as single items, not flattened into characters. The function must be a generator and should use `yield from` for delegation.

Constraints

- Each argument can be any iterable (list, tuple, set, etc.) of any depth. - The total number of elements to yield will not exceed 10^4. - Strings are atomic; do not flatten them into characters. - The function must return a generator object.

Example

>>> list(flatten_deep([1, [2, 3], 4]))
[1, 2, 3, 4]
>>> list(flatten_deep([1, [2, [3, [4]]]], 5))
[1, 2, 3, 4, 5]
>>> list(flatten_deep(['ab', ['cd']]))
['ab', 'cd']
>>> gen = flatten_deep([])
>>> list(gen)
[]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `yield from` to delegate to a recursive helper or to the main function itself.
Check if an element is iterable using `isinstance(element, (list, tuple, set))` but exclude strings.
For non-iterable elements, just `yield` them individually.
The `yield from` keyword can be used inside a loop to yield from nested iterables.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.