Search code examples
pythonyield

Where to use yield in Python best?


I know how yield works. I know permutation, think it just as a math simplicity.

But what's yield's true force? When should I use it? A simple and good example is better.


Solution

  • yield is best used when you have a function that returns a sequence and you want to iterate over that sequence, but you do not need to have every value in memory at once.

    For example, I have a python script that parses a large list of CSV files, and I want to return each line to be processed in another function. I don't want to store the megabytes of data in memory all at once, so I yield each line in a python data structure. So the function to get lines from the file might look something like:

    def get_lines(files):
        for f in files:
            for line in f:
                #preprocess line
                yield line
    

    I can then use the same syntax as with lists to access the output of this function:

    for line in get_lines(files):
        #process line
    

    but I save a lot of memory usage.