given the simple examples we're using to illustrate fundamentalsyou might be wondering just why you' ever care to code generator at all in this section' examplefor instancewe could also simply build the list of yielded values all at oncedef buildsquares( )res [for in range( )res append( * return res for in buildsquares( )print(xend=' for that matterwe could use any of the for loopmapor list comprehension techniquesfor in [ * for in range( )]print(xend=' for in map((lambda nn * )range( ))print(xend=' howevergenerators can be better in terms of both memory use and performance in larger programs they allow functions to avoid doing all the work up frontwhich is especially useful when the result lists are large or when it takes lot of computation to produce each value generators distribute the time required to produce the series of values among loop iterations moreoverfor more advanced usesgenerators can provide simpler alternative to manually saving the state between iterations in class objects--with generatorsvariables accessible in the function' scopes are saved and restored automatically we'll discuss class-based iterables in more detail in part vi generator functions are also much more broadly focused than implied so far they can operate on and return any type of objectand as iterables may appear in any of ' iteration contextsincluding tuple callsenumerationsand dictionary comprehensions interestinglygenerator functions are also something of "poor man'smultithreading device--they interleave function' work with that of its callerby dividing its operation into steps run between yields generators are not threadsthoughthe program is explicitly directed to and from the function within single thread of control in one sensethreading is more general (producers can run truly independently and post results to queue)but generators may be simpler to code see the footnote in for brief introduction to python multithreading tools note that because control is routed explicitly at yield and next callsgenerators are also not backtrackingbut are more strongly related to coroutines--formal concepts that are both beyond this scope generator functions and expressions