PYTHON

# Python Generators

Article by:
 Date Published: March 25, 2019 Last Modified: March 25, 2019

## Overview

Python generators are a type of iterator in Python, with the important caviat that you can only iterate over them once.

They are created by writing a function what uses the keyword yield rather than return.

## Type Annotations

Generators can be annotated with the Generator type that is defined in the typing module:

 1  Generator[yield_type, send_type, return_type] 

## Code Examples

The following code example chunks up a list:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  def chunkify(l: List[Any], n: int) -> Generator[List[Any], None, None]: """ Provides a generator which provides chunks of size n of the list. Args: l: The list to chunk up. n: The number of list elements to include in each chunk. Each chunk will be of length n, except for the last chunk, which may be from 1 to length n depending on the number of remaining elements. Returns: A generator which, when iterated over, provides the chunks. """ for i in range(0, len(l), n): # Make sure we don't go over the bounds of the iterable on the last chunk upper_bound = min(i + n, len(l)) yield l[i : upper_bound]

## Authors

### Geoffrey Hunter

Dude making stuff.