Site Tools


Python yield


>>> list = [1,2,3,4,5,6]
list is iterable
>>> list = [x for x in range(7)]
Iterables are handy but stores all values in memory


>>> listgenerator = (x for x in range(7))
>>> for i in listgenerator: print(i)

Generators are iterators, but you can only iterate over them once. It's because they do not store all the values in memory, they generate the values on the fly.


Yield is a keyword that is used like return, except the function will return a generator.

def createGenerator():
    print("not running until next() called")
    for i in range(7):
       yield i*i
x = createGenerator()
for t in x:

Practical example

tail -f access.log

import time
def follow(thefile):,2) # Go to the end of the file
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1) # Sleep briefly
        yield line
logfile = open("access-log")
for line in follow(logfile):


def grep(pattern,lines):
    for line in lines:
        if pattern in line:
            yield line
# Set up a processing pipe : tail -f | grep python
logfile = open("access-log")
loglines = follow(logfile)
pylines = grep("python",loglines)
# Pull results out of the processing pipeline
for line in pylines:
    print line,
python/yield.txt · Last modified: 2020-04-19 (external edit)