A good example that comes to mind is Python's range() function. In Python 2, if you wrote: for i in range(1000000): if some_condition: break the range() function would produce a list of 1,000,000 consecutive integers. The code would then iterate through the list until some_condition was satisfied. The list would occupy something like 8 MB of RAM, and it would take time for Python to produce the numbers and construct the full list, even if some_condition was satisfied early. One the other hand, the range() function in Python 3 is like a generator, so when you iterate over it as in the example above, the integers are generated on the fly. RAM usage is obviously very much less, no time is wasted constructing a list, and if the code exits early Python would not have wasted time producing hundreds of thousands of unused values. Answer from JamzTyson on reddit.com
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_generators.asp
Python Generators
Python Examples Python Compiler ... resume their execution. When a generator function is called, it returns a generator object, which is an iterator....
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ generator
Python Generators (With Examples)
... Created with over a decade of experience and thousands of feedback. ... Try Programiz PRO! ... Become a certified Python programmer. Try Programiz PRO! ... In Python, a generator is a function that returns an iterator that produces a sequence of values when iterated over.
๐ŸŒ
Real Python
realpython.com โ€บ introduction-to-python-generators
How to Use Generators and yield in Python โ€“ Real Python
July 16, 2024 - Introduced with PEP 255, generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ generators-in-python
Generators in Python - GeeksforGeeks
A generator function is a special type of function that returns an iterator object. Instead of using return to send back a single value, generator functions use yield to produce a series of results over time.
Published ย  December 12, 2025
๐ŸŒ
Learn Python
learnpython.org โ€บ en โ€บ Generators
Generators - Learn Python - Free Interactive Python Tutorial
Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ can someone please explain what is a generator and why do we really need it?
r/learnpython on Reddit: can someone please explain what is a generator and why do we really need it?
August 2, 2023 -

So far I have I just know the theoretical definition like: a generator is a special type of iterator that allows you to generate a sequence of values on-the-fly, without storing them in memory all at once.

But I'm unable to understand the practical use of it like why do we need generator?

๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ python โ€บ python-generator
Python Generator Functions
A generator is a special type of function which does not return a single value, instead, it returns an iterator object with a sequence of values. In a generator function, a yield statement is used rather than a return statement.
๐ŸŒ
Quora
quora.com โ€บ What-does-a-generator-return-in-Python
What does a generator return in Python? - Quora
Answer (1 of 2): Generator functions are a special kind of function that returns a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.
Find elsewhere
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ python-generators
Basics of Python Generator Functions | Built In
The __next__ method will then return the next value in the sequence and might possibly raise the StopIteration exception when there are no values to be returned. As you can see, the process of creating iterators is lengthy, which is why we turn to generators. Again, Python generators are a simple way of implementing iterators.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-generators
Python Generators
August 29, 2023 - The code above is similar to the previous ones, but calls each value yielded by the generator with the function next(). In order to do this, we must first instantiate a generator g, which is like a variable that holds our generator state. When the function next() is called with the generator as its argument, the Python generator function is executed until it finds a yield statement. Then, the yielded value is returned to the caller and the state of the generator is saved for later use.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ generator
Python Generator: Syntax, Usage, and Examples
Python generators allow you to create iterators in an efficient and memory-friendly way. Unlike regular functions, which return a single value and terminate, a generator can yield multiple values one at a time, suspending execution between each.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ explain it to me like iโ€™m 5: yield and generators
r/learnpython on Reddit: Explain it to me like Iโ€™m 5: Yield and Generators
October 6, 2022 -

Explain it to me like Iโ€™m 5: Yield and Generators

For some reason, despite relatively thorough internet surfing I cannot seem to grasp this concept.

Does anyone have a good explanation for it?

Top answer
1 of 4
61
I made a YouTube video explaining this very idea: https://www.youtube.com/watch?v=evaWHEuoTPk&t=1s
2 of 4
35
I guess my best explanation is that generators are (often) "reusable loops". You can define a loop structure, and then use that wherever you need to loop over something. yield is still likely the easiest to understand as a return that doesn't end the function. Say you had something like def infinite_n(n: int): while True: yield n for num in infinite_n(1): print(num) # Keeps printing 1 forever This could just as well be written as while True: print(1) but now you can't reuse the while-loop. I know that was a very rudimentary example, so let's try the good ol' Fibonacci sequence next. def fib_gen(): a, b = 0, 1 while True: yield a a, b = b, a+b for num in fib_gen(): print(num) # Another infinite loop The non-reusable version would be a, b = 0, 1 while True: print(a) a, b = b, a+b and now we'd actually need to write this same loop over and over if we wanted to use this elsewhere. In a nutshell, generators let us write reusable loops that we can then use with compact for-loops, where we don't need to understand how they work. It's a bit like how you've probably gotten used to using range in loops - they're technically iterators and not generators, but generators are a subset of iterators so it's not like they're totally different. Sure, you could write your own loop that increments an integer every time, but why bother when you already have a for-loop that can do exactly that for you?
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ python-generators
A Complete Guide to Python Generators | Codecademy
Each time yield is encountered, the generator produces a value and pauses execution, preserving its state until the next value is requested. ... To understand the above syntax, letโ€™s look at an example.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-are-generators-in-python-and-how-to-use-them
What are generators in Python, and how to use them?
Generators are a powerful feature in Python, allowing efficient and memory-friendly iteration over large or infinite sequences. Using the yield keyword, a generator produces values one at a time without storing the entire sequence in memory, enabling lazy evaluation. Generators provide simpler syntax compared to custom iterators and can handle large datasets more effectively by generating values only when needed. Unlike normal functions, which return values and exit, generators can pause and resume their state.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2618134 โ€บ what-is-generator-in-python
What is Generator In Python | Sololearn: Learn to code for FREE!
Kartik Taneja Generator is a function which "yields" one value at a time. It is helpful when we need to iterate over objects. It basically returns one value at a time. This is is really useful in many cases.
๐ŸŒ
Medium
medium.com โ€บ @farihatulmaria โ€บ what-is-a-generator-in-python-how-is-it-different-from-a-regular-function-6ca01e961f42
What is a generator in Python? How is it different from a regular function? | by Farihatul Maria | Medium
July 21, 2024 - Unlike regular functions that return a single value and then terminate, generators can yield multiple values, one at a time, pausing their state between each yield. This makes generators highly memory efficient for handling large datasets or ...
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ python-generators-understanding-power-different-use-cases-singh
Python Generators: Understanding the Power and Different Use Cases
April 22, 2023 - When this function is called with an argument n, it creates a generator object that generates the numbers from 1 up to n. When the generator object is iterated over, it returns the values produced by the yield statement.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-generators
Python Generators: Boosting Performance and Simplifying Code | DataCamp
February 14, 2025 - Think of generators as a conveyor belt in a factory: Instead of stacking all the products in one place and running out of space, you process each item as it comes down the line. This makes generators memory-efficient and a natural extension of Python's iterator protocol, which underpins many of Python's built-in tools like for loops and comprehensions. The magic behind generators lies in the yield keyword. Unlike return, which outputs a single value and exits the function, yield produces a value, pauses the function's execution, and saves its state.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ python โ€บ python generators
Python Generators - Learn and Master
February 21, 2009 - The generator in Python is a special type of function that returns an iterator object. It appears similar to a normal Python function in that its definition also starts with def keyword.