‘Return’ vs ‘Yield’ in Python

return_yield

ARTICLE SUMMARY

Indhumathy shares with us a short guide to what the ‘return’ and ‘yield’ statements actually do in Python.

In Python functions, we might have seen ‘return’ or ‘yield’ statements. What does the ‘return’ and ‘yield’ statement do? 

Let’s look at this in detail in this article. 

‘return’ statement

The ‘return’ statement terminates the function call and returns the value to the caller. ‘return’ statement occurs only inside the function. 

Syntax 

return [expression_list] 

expression_list is evaluated and it can return a tuple/list/dict/set or a single value.

Python

1) return a single value

Python

2) ‘return’ →List

Python

3) return →a tuple

If the expression list contains at least one comma, it returns a tuple.

Python

4) return →dict

Python

Multiple return statements 

When we have multiple return statements, the function call is terminated when it reaches the first return statement. 

Python

‘yield’ statement

If a function has a ‘yield’ statement instead of a ‘return ‘ statement, it is known as generator functions or generators.  

‘yield statement’ won’t terminate the function during the function call. During multiple calls, it will generate successive output.  

The generator function will return a generator object. Generator objects are single active iterators. They only support one active iteration. We can access the generator object using for loop, list(),next() 

1. Generators 

During a function call, the generator function will return a generator object which yields one result at a time. 
Generators are memory efficient. 

Python

2. Using next() 

We can access the result one by one from the generator object using next(). It will yield value one by one. 
When the object is exhausted, it will raise “Stop Iteration” 

Python

3. Using for loop 

We can access the generator object using for loop also. It will loop through the result. To overcome “StopIteration”, we can use for loop to access the elements from the generation object. 

Python

4. Using list() constructor 

We can convert the generator object into a list using the list() constructor. 

Python

‘yield’ statement vs ‘return’ statement 

The generator object will yield one value at a time. It’s like resuming function. 

‘return’ statement will return all the values at one time. 

Python

Difference between Normal function and Generator Function 

  1. Normal function when called, compute the value and return it.
    The generator function returns a generator object which is an iterator. 
  2. A normal function has a ‘return’ statement. 
    Generator function has ‘yield statement 
  3. ‘return’ stops the execution but ‘yield’ pauses the execution and resumes at the same point. 
  4. Generators are memory efficient. 

Conclusion 

In this article, I have covered the differences between the ‘return’ statement and ‘yield’ statement. In normal function,’ return’ is used but ‘yield’ is used in the generator function. I hope you all like it, thanks for reading!

In Python, the return statement sends back a result from a function to wherever the function was called. After Python hits a return statement, the function ends immediately and gives back the value.

The return statement in Python exits a function and provides a value or result back to the caller. This allows you to use the function’s result in other parts of your program.

In Python, you can return multiple values by separating them with commas. Python automatically packs these values into a tuple. For example:

def get_coordinates():
return 10, 20

x, y = get_coordinates()

Use the return statement at the end of a function to output results or data. Here’s an example:

def add_numbers(a, b):
return a + b

result = add_numbers(3, 4)
print(result) # Output: 7

In Python, the yield keyword is used within generators to pause the function’s execution and send back a value. Unlike return, a function using yield can resume later to produce more values.

Yield pauses a function’s execution and returns a value temporarily. This allows the function to produce multiple values over time without completely exiting.

Yield works by creating a generator, a special function type. Each time the generator’s next item is requested, it resumes from where it paused (after the last yield statement), allowing efficient memory use especially for large datasets or streams.

The key difference is that return exits a function completely after giving back a single value, while yield pauses the function, returning one value at a time and allowing the function to resume where it left off. This makes yield ideal for generators and large data sequences.

RELATED ARTICLES

From defining your priorities and tailoring your CV to optimising your online presence and making the most of networking, Bethany Windsor from Generation Logistics, offers...
Silvia Sparry, Global Chief Transformation Officer at GroupM Nexus, reflects on her 20-year journey in the tech and advertising industry, sharing five key lessons that...
As technology evolves and hybrid work continues to rise, collaboration is becoming more essential than ever. Norma Lovhaugen, Head of Product Marketing, Strategy and Design...
Laura Ashley-Timms, a performance improvement coach and author of The Answer is a Question, debunks common leadership myths and shares expert insights on how to...

Join Our Community

Download Our App