← Back to Scratch Pad

Python Generators

A generator is a function that returns a list - sort of. It seems like it is returning a list (you can kinda treat the function as a list) but it is really returning an item instead of a list on each invocation. The function maintains state, and so the next time it is invoked, it returns the next item from the list. 

Why use it? Because it allows you to process one item at a time, instead of having to process all the items - combining them into a list - and returning that list. When you have to process very large amounts of data - maybe more than you can store in memory - generators are the way to go. 

Instead of using the return key word, use the yield keyword.

def my_generator() :
  print ("calling generator")
  for x in [1, 2, 3, 4] :     # Assume that this is an expensive operation, or the list is huge > memory available
    print ("yielding next value")
    yield x

for item in my_generator() : 
  print (item)

calling generator 
yielding next value 
1 
yielding next value 
2 
yielding next value 
3 
yielding next value 
4

 

 

Ref: 

Python generators – saving time and memory | I gotta have my orange juice. (scottmoonen.com)