What Are Generators?
Generators in Python are a simple way to create iterators. Instead of building a list and storing all elements in memory at once, generators calculate each item on the fly, which saves memory and improves performance when dealing with large datasets.
Creating Generators
One of the simplest ways to create a generator is by using generator expressions. These are similar to list comprehensions, but instead of square brackets [], we use parentheses ().
>>> sample_list = [x ** 2 for x in range(10)]
>>> sample_list
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> gen = (x ** 2 for x in range(10))
>>> gen
<generator object <genexpr> at 0x1022ef630>
Using Generators
Once a generator is created, it can be iterated using a for loop. Python automatically handles the end of iteration and stops when there are no more items to return.
gen = (x ** 2 for x in range(10))
for value in gen:
print(value)
Alternatively, we can manually retrieve items from a generator using the __next__() method.
gen = (x ** 2 for x in range(10))
print(gen.__next__())
print(gen.__next__())
print(gen.__next__())
print(gen.__next__())
Generators vs. Iterators
Generators are functions that return iterator objects. They simplify the process of creating iterators by automatically managing the internal state and handling the iteration protocol.
Creating Iterators
We can also convert iterable objects like lists, tuples, and strings into iterators using the built-in iter() function.
numbers_list = [1, 2, 3, 4]
numbers_tuple = (1, 2, 3)
text_string = 'Python'
print(iter(numbers_list))
print(iter(numbers_tuple))
print(iter(text_string))