In Python, to create iterators, we can use both regular functions and generators. Generators are written just like a normal function but we use yield() instead of return() for returning a result. It is more powerful as a tool to implement iterators. It is easy and more convenient to implement because it offers the evaluation of elements on demand.
Unlike regular functions which on encountering a return statement terminates entirely, generators use a yield statement in which the state of the function is saved from the last call and can be picked up or resumed the next time we call a generator function. Another great advantage of the generator over a list is that it takes much less memory.
Generator expressions in Python are a concise and efficient way to create an iterator, which is a data structure that can be traversed sequentially. Unlike list comprehensions, which create a list in memory, generator expressions produce a generator object, which generates the elements of the sequence on-the-fly as they are requested.
Generator expressions are enclosed in parentheses and consist of an expression followed by a for clause and an optional if clause. The expression is evaluated for each item in the iterable specified in the for clause, and the results are yielded one at a time as the generator is iterated over. The if clause is used to filter the items produced by the expression.
Here's an example of a generator expression that generates the first ten even numbers
even_numbers = (i for i in range(20) if i % 2 == 0)
In this example, the expression i for i in range(20) if i % 2 == 0
generates the even numbers from 0 to 18, and the resulting generator object is assigned to the variable even_numbers
. To access the elements of the generator, you can use a for loop or the next()
function.
Generator expressions are useful for creating large sequences that would be memory-intensive if stored in a list, or for generating sequences on-the-fly in a memory-constrained environment. They can also be used in conjunction with other Python functions that accept iterators, such as sum()
, max()
, and min()
.