Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. yield / Generator Functions

yield / Generator Functions

A function that uses yield becomes a generator function, and calling it returns a generator object. Generators do not load all elements into memory at once — they produce values one at a time on demand (lazy evaluation). When working with large amounts of data, generators can save significantly more memory than lists.

Syntax

# Generator function (a function containing yield)
def my_generator():
    yield value1
    yield value2

# Generator expression
gen = (expression for variable in iterable)

# yield from (delegate to another iterable)
def delegating_gen():
    yield from other_generator()

Syntax Overview

SyntaxDescription
yield valueReturns a value to the caller and pauses execution at that point.
yield from iterableYields each element from another iterable (including generators).
(expression for x in iter)Generator expression. A memory-efficient alternative to a list comprehension — use () instead of [].
next(gen)Retrieves the next value from the generator one at a time.
list(gen)Expands all elements of the generator into a list.

Sample Code

# Basic generator function
def count_up(start, end):
    """Yields numbers from start to end one at a time"""
    current = start
    while current <= end:
        yield current
        current += 1

gen = count_up(1, 5)
print(next(gen))    # 1
print(next(gen))    # 2
print(next(gen))    # 3

for n in count_up(1, 5):
    print(n, end=' ')   # 1 2 3 4 5

# Memory usage comparison
import sys

# List: holds everything in memory at once
big_list = [x ** 2 for x in range(10**6)]
print(sys.getsizeof(big_list))  # ~8MB or more

# Generator: lazy evaluation uses far less memory
big_gen = (x ** 2 for x in range(10**6))
print(sys.getsizeof(big_gen))   # ~120 bytes (always constant)

# yield from: delegating to an iterable
def flatten(nested):
    """Generator that flattens a nested list"""
    for item in nested:
        if isinstance(item, list):
            yield from flatten(item)    # Flatten recursively
        else:
            yield item

data = [1, [2, 3], [4, [5, 6]], 7]
print(list(flatten(data)))  # [1, 2, 3, 4, 5, 6, 7]

# Infinite generator (use with itertools or slicing)
def fibonacci():
    """Generates an infinite Fibonacci sequence"""
    a, b = 0, 1
    while True:
        yield a
        a, b = b, a + b

import itertools
first_10 = list(itertools.islice(fibonacci(), 10))
print(first_10)  # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

# Chaining generator expressions
lines = ['  hello  ', '  world  ', '  python  ']
stripped = (line.strip() for line in lines)
upper    = (s.upper() for s in stripped)
print(list(upper))  # ['HELLO', 'WORLD', 'PYTHON']

Details

A generator is an object that automatically implements the iterator protocol (__iter__ and __next__). When execution reaches a yield statement, the function pauses and returns the value to the caller. The next call to next() resumes execution from immediately after that yield. When the function exits, a StopIteration exception is raised to signal the end of iteration.

yield from was added in Python 3.3. It delegates iteration to another generator or iterable, and is more concise than using a nested for loop with yield. It also forms the foundation of asynchronous programming with async/await, allowing generators to be used as coroutines.

A generator cannot be reused once it has been exhausted. To iterate over the values again, either convert the output to a list beforehand, or call the generator function again to create a new generator object.

If you find any errors or copyright issues, please .