Last modified: Nov 19, 2026

Python Yield vs Return: Key Differences

Python yield and return both send values back to the caller. But they work in very different ways. This guide explains both keywords with simple examples.

You will see how each one behaves, when to use it, and why the choice matters. By the end, you will know exactly which one your function needs.

What Does return Do in Python?

The return keyword ends a function. It sends a value back to the caller. After that, the function is finished.

Every call starts fresh. Nothing is remembered from the previous call. This is the normal behavior you expect from most functions.


def add(a, b):
    return a + b  # function ends here

result = add(2, 3)
print(result)

5

If a function has no return, Python returns None by default. A return with no value also gives back None.

You can also return several values at once. Python packs them into a tuple.


def stats(numbers):
    return min(numbers), max(numbers)  # returns a tuple

low, high = stats([4, 9, 2])
print(low, high)

2 9

What Does yield Do in Python?

The yield keyword turns a function into a generator. A generator produces values one at a time, not all at once.

When the function hits yield, it pauses. It saves its state and sends one value out. The next call resumes right after that line.

This lazy behavior saves memory. You never build the full list in memory. You produce items only when they are needed.


def count_up(n):
    for i in range(n):
        yield i  # pause here and send one value

for num in count_up(3):
    print(num)

0
1
2

Notice that count_up(3) does not run the loop right away. It returns a generator object. The loop runs only when you iterate over it.

If you want a deeper walkthrough, read this guide on Python generators and yield.

Return vs Yield: Side by Side

The table below sums up the main differences.

  • return sends one value and ends the function.
  • yield sends one value and pauses the function.
  • return gives back a normal object.
  • yield gives back a generator object.
  • return uses memory for the whole result.
  • yield uses memory for one item at a time.

Here is the same task done both ways. First with return:


def squares_list(n):
    result = []
    for i in range(n):
        result.append(i * i)  # builds full list
    return result

print(squares_list(5))

[0, 1, 4, 9, 16]

Now the same task with yield:


def squares_gen(n):
    for i in range(n):
        yield i * i  # one value at a time

print(list(squares_gen(5)))

[0, 1, 4, 9, 16]

The output looks the same. But the generator never stores the full list. That difference grows huge with large data.

How to Tell Them Apart

Look for the keyword inside the function body. If you see yield anywhere, the function is a generator.

A generator function can also use return. But that return just stops the generator. It does not send a value to the loop.


def limited():
    yield 1
    yield 2
    return  # stops the generator, sends no value
    yield 3  # never reached

print(list(limited()))

[1, 2]

This is a common trap for beginners. Mixing both keywords without care can confuse your logic.

When Should You Use return?

Use return for most functions. It is simple and predictable. It fits small results and one-time answers.

Pick it when you need the full result at once. Sorting, math, and string work usually need the whole value.

It also fits functions that call other helpers. For example, functions that use Python *args and **kwargs often return a combined result.

When Should You Use yield?

Use yield when the data is large or endless. It keeps memory low and starts work fast.

Generators shine when you process items one by one. Reading big files line by line is a classic case.


def read_lines(path):
    with open(path) as f:
        for line in f:
            yield line.strip()  # one line at a time

for line in read_lines("data.txt"):
    print(line)

This code never loads the whole file into memory. It works fine even for huge files.

Generators also pair well with other tools. For example, you can combine them with Python any() and all() to stop early on the first match.

Common Mistakes with yield

Many beginners try to call a generator like a normal function. They expect a value, not a generator object.


def nums():
    yield 1
    yield 2

print(nums())  # not the values!

To get the values, loop over the generator or wrap it in list(). Also remember that a generator runs only once. After it is used up, it is empty.

If you need to loop again, create a new generator. Or store the items in a list if memory allows.

Generators and Recursion

Generators can call themselves. This is handy for walking trees or nested data.


def flatten(items):
    for item in items:
        if isinstance(item, list):
            yield from flatten(item)  # recursive generator
        else:
            yield item

print(list(flatten([1, [2, [3, 4]], 5])))

[1, 2, 3, 4, 5]

The yield from statement delegates to another generator. It is cleaner than a manual loop. For more on self-calling functions, see Python recursion with examples.

Quick Decision Guide

Ask yourself these questions before you choose.

  • Do I need all results at once? Use return.
  • Is the data huge or endless? Use yield.
  • Do I need lazy, step-by-step output? Use yield.
  • Do I need a simple, reusable value? Use return.

Most of the time, return is the right pick. Reach for yield when memory or speed demands it.

Conclusion

Python yield and return both send values back. But they solve different problems.

return ends a function and gives a full result. yield pauses a function and gives one value at a time.

Start with return for normal code. Switch to yield when you need lazy, memory-friendly output. With these examples in hand, you can pick the right tool every time.