What Is a Generator in Python?
A generator in Python is an object that produces values one at a time, only when they are needed.
This is useful because Python does not have to create and store every value at once. For beginners, the main idea is simple: a generator lets you work through data step by step instead of building a full list immediately.
Generators are commonly created with a function that uses yield.
Simple definition
A generator is an object that gives values one at a time when Python asks for them.
Key points:
- It does not build the whole result at once
- It can be looped over with
for - It is often created by a function that uses
yield
Quick example
This is the simplest generator example:
def numbers():
yield 1
yield 2
yield 3
for n in numbers():
print(n)
Output:
1
2
3
What happens here:
numbers()is a generator function because it usesyield- Each
yieldgives back one value - The
forloop asks for values one by one
Why beginners should care
Generators are helpful because they:
- Work well when looping through many values
- Can use less memory than a full list
- Show how Python can process items step by step
You do not need generators for every task. But they are useful when you do not want to store everything at once.
How a generator works
A normal function usually finishes with return.
A generator function is different. It uses yield to produce one value at a time.
def greet():
yield "Hello"
yield "World"
for word in greet():
print(word)
Output:
Hello
World
The important idea is this:
- Python runs the function until it reaches
yield - It sends back that value
- The function pauses
- When the next value is needed, the function continues from where it paused
That pause-and-continue behavior is what makes generators special.
Generator function example
Here is a small example that shows the function pausing after each yield:
def count_up():
print("Start")
yield 1
print("Middle")
yield 2
print("End")
yield 3
for number in count_up():
print("Got:", number)
Output:
Start
Got: 1
Middle
Got: 2
End
Got: 3
Notice what happens:
- The function does not run all at once
- It stops at each
yield - It continues only when the loop asks for the next value
If you want a fuller beginner lesson, see generators in Python explained.
Generator vs list
A generator and a list are not the same.
List
A list stores all items in memory right away.
numbers = [1, 2, 3, 4]
print(numbers)
print(numbers[0])
Generator
A generator produces items only when needed.
def numbers():
yield 1
yield 2
yield 3
yield 4
gen = numbers()
for item in gen:
print(item)
Main differences:
- A list stores everything at once
- A generator produces values one at a time
- A list can be reused easily
- A generator is usually used up as you iterate through it
If you want all generator values at once, you can convert it with list():
def numbers():
yield 1
yield 2
yield 3
gen = numbers()
all_values = list(gen)
print(all_values)
Output:
[1, 2, 3]
When to use a generator
A generator is useful when you want to process values step by step.
Common cases:
- Reading large data one piece at a time
- Looping through many values without storing them all
- Creating very large sequences
For small data, a list is often simpler. For large or streaming data, a generator can be a better choice.
Important beginner note
A generator is iterable, but it is not a list.
That means:
- You can loop over it
- You usually cannot access items by index
- It may be exhausted after one full loop
This will fail:
def numbers():
yield 10
yield 20
yield 30
gen = numbers()
print(gen[0])
This causes an error because generators do not support indexing.
If you need indexing, convert the generator to a list first:
def numbers():
yield 10
yield 20
yield 30
gen = numbers()
values = list(gen)
print(values[0])
Output:
10
Generator expression
You can also create a generator with parentheses.
squares = (x * x for x in range(5))
for value in squares:
print(value)
Output:
0
1
4
9
16
This is called a generator expression.
It looks similar to a list comprehension, but it does not build a full list right away. If you want to compare the two, see list comprehensions in Python explained.
Related concepts
These terms are closely connected:
- Iterable: something you can loop over
See what is an iterable in Python - Iterator: an object that returns the next item
See what is an iterator in Python - List comprehension: creates a list, unlike a generator expression
A generator is part of Python’s iteration system, but for beginners, the main thing to remember is that it gives values one by one.
Common mistakes
Beginners often run into these problems:
- Thinking a generator is the same as a list
- Trying to access a generator with an index like
my_gen[0] - Forgetting that a generator may be empty after one full loop
- Using
returnwhenyieldis needed
Example: generator gets exhausted
def numbers():
yield 1
yield 2
yield 3
gen = numbers()
for item in gen:
print(item)
print("Loop again:")
for item in gen:
print(item)
Output:
1
2
3
Loop again:
The second loop prints nothing because the generator has already been used up.
Useful checks
These simple commands can help you understand what a generator is doing:
def numbers():
yield 1
yield 2
yield 3
my_generator = numbers()
print(type(my_generator))
print(next(my_generator))
print(list(my_generator))
Possible output:
<class 'generator'>
1
[2, 3]
Why this output matters:
type(my_generator)shows that the object is a generatornext(my_generator)gets the next valuelist(my_generator)collects the remaining values into a list
You can also inspect it with a loop:
def numbers():
yield 1
yield 2
yield 3
my_generator = numbers()
for item in my_generator:
print(item)
FAQ
What is a generator in Python in simple words?
It is an object that gives values one at a time instead of storing them all at once.
What does yield do in Python?
yield sends back one value and pauses the function until the next value is requested.
Is a generator the same as a list?
No. A list stores all items immediately, while a generator produces items as needed.
When should I use a generator?
Use one when you want to loop through many values without keeping all of them in memory at once.
Can I convert a generator to a list?
Yes. Use list(generator_object) if you want all values at once.
See also
- Generators in Python explained
- What is an iterator in Python
- What is an iterable in Python
- List comprehensions in Python explained
- Python
list()function explained
If this topic makes sense now, the best next step is to read a beginner-friendly lesson that compares generators, iterables, and iterators side by side with simple examples.