Generators

Preview: Anonymous contributor 108 total contributions
Anonymous contributor's avatar
Anonymous contributor

108 total contributions

Anonymous contributor's avatar
Anonymous contributor
Published May 4, 2022
Contribute to Docs

In Python, a generator is a function or expression that will process a given iterable one object at a time, on demand. A generator will return a generator object, which can be cast as a list or some other data structure as appropriate.

Generators

Generators are a convenient means of employing iterator functionality within the syntax of a function or expression. One of the main advantages of generators is that they evaluate items on demand, which means only one item is in memory at a time in lieu of the entire sequence (as with a list).

A Generator Function

The following code shows the creation of a generator function. In the function definition, the yield statement is used to return or include an item in the final generator object.

def return_evens(lst):
for l in lst:
if l % 2 == 0:
yield l
eggs = [x for x in range(20)]
print(list(return_evens(eggs)))
# Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Copy to clipboard
Copy to clipboard

A Generator Expression

The functionality in the previous example can alternatively be defined as an expression. A generator expression utilizes the same syntax as a list comprehension with parentheses framing the statement instead of square brackets.

eggs = [x for x in range(20)] # a list comprehension
list((x for x in eggs if x % 2 == 0)) # a generator expression
Copy to clipboard
Copy to clipboard

Custom Iteration

A generator object can be incrementally advanced with the next() function. When next() is called the current item is returned and the state is saved.

Visit us
Visit us
Hide code
Code
Output
Hide output
Loading...
Copy to your clipboard

All contributors

  1. Preview: Anonymous contributor 108 total contributions
    Anonymous contributor's avatar
    Anonymous contributor

    108 total contributions

  1. Anonymous contributor's avatar
    Anonymous contributor

Contribute to Docs

Learn Python on Codecademy

Morty Proxy This is a proxified and sanitized view of the page, visit original site.