In programming, generators are essentially functions that returns an array (it takes in parameters, can be called, and generates a sequence of values) but yields the values one at a time (i.e., it behaves like an iterator). This requires much less memory to execute.

# example from "Functional Programming in Python"
def get_log_lines(log_file):
	line = read_line(log_file)
	while True:
		try:
			if complex_condition(line):
				yield line
			line = read_line(log_file)
		except StopIteration:
			raise
 
log_lines = get_log_lines(huge_log_file)

Comprehensions

Generator comprehension uses similar syntax as list comprehension, just without square brackets. Below is the same functionality as above:

log_lines = (line for line in read_line(huge_log_file) if complex_condition(line))