nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
4 of 8
1Why is it important to use functools.wraps when writing a decorator?
2What is the difference between a class-based decorator and a function-based decorator?
3How does the yield keyword turn a function into a generator, and what happens to the function's state between calls?
4What is the difference between yield and yield from?
5What is a decorator, and how does it use closures to wrap a function's behavior?
6What is the difference between an iterable and an iterator? What protocol must an object implement to be an iterator?
7How would you write a decorator that accepts arguments (e.g., @retry(times=3))?
8How would you process a very large file that doesn't fit in memory using generators?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
04 / 08

What is the difference between yield and yield from?

Difficulty: 8/10
Generators, Yield From, Delegation

yield from delegates the full iterator protocol to a sub-iterator

yield from iterable is generator delegation introduced by PEP 380. It does not just loop: it forwards send(), throw(), and close() to the sub-iterator, and the value the sub-iterator returns via StopIteration becomes the value of the yield from expression. A manual for loop over the sub-iterator yields the same items but silently drops all of that: send() values never reach the inner generator, exceptions are not thrown into it, and its return value is lost. So yield from is the correct primitive when you are delegating to a sub-generator, and a for loop is correct when you want to transform each item.

  1. 1

    Use yield from for flattening, recursive generator walkers, and any delegation where the inner generator may be sent values or has a meaningful return value.

  2. 2

    Use an explicit for loop with yield when you need to inspect, filter, or transform each item, or when you want to instrument the flow.

  3. 3

    Trade-off: yield from makes the delegation opaque, which is great for correctness and bad for per-item logging or profiling.

  4. 4

    Common mistake: writing a for loop wrapper around a coroutine-style generator and expecting send() to work through it. It will not.

  5. 5

    Common mistake: assuming yield from accepts any iterable for send purposes; delegation only forwards send/throw when the sub-iterator supports them.

  6. 6

    Version note: yield from arrived in Python 3.3 (PEP 380). It is a SyntaxError inside async def functions and async generators, where await and async for are the equivalents.

javascript

Scenario Questions

0-2 years experience

  1. 1You need to flatten a list of lists lazily. How does yield from simplify the code?
  2. 2What value does the expression 'yield from xs' evaluate to?

2-5 years experience

  1. 1You wrap a sub-generator in a for loop instead of yield from. What behavior do you lose?
  2. 2How do you retrieve the return value of a sub-generator that finishes with return?

5-8 years experience

  1. 1You are building a recursive tree walker with generators. Why is yield from usually preferred over manual recursion?
  2. 2You need per-item instrumentation across a nested generator chain. Why might yield from make that harder, and how do you work around it?

8+ years experience

  1. 1Design a delegation layer that correctly forwards close() and throw() into a pool of sub-generators and handles partial consumption.
  2. 2You are porting generator-based coroutine code to async/await. What does yield from map to and where does the mapping break down?

Follow-up Questions

  • How does a manual for-loop delegation break a generator that expects send()?
  • Why is yield from a SyntaxError inside an async def, and what replaces it?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.