Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python yield from code

Practice delegating generator output, continuing after delegation, capturing a subgenerator return value, and composing generator layers.

Intermediate ~9 min read

Challenge 1

Delegate the list's values from this generator.

python Your answer
def values():

print(list(values()))

Press Ctrl⌘Enter to check your answer

Challenge 2

Delegate the first two states, then yield the final state yourself.

python Your answer
def states():

print(list(states()))

Press Ctrl⌘Enter to check your answer

Challenge 3

Capture the subgenerator's return value in result.

python Your answer
def child():
    yield "work"
    return "done"

def parent():

print(list(parent()))

Press Ctrl⌘Enter to check your answer

Challenge 4

Make outer delegate to middle so the source values reach the caller.

python Your answer
def source():
    yield from [10, 20]

def middle():
    yield from source()

def outer():

print(list(outer()))

Press Ctrl⌘Enter to check your answer