Here is a simple tip for Python programming…
Ever write code that looks like this?
def foo(): result =  for x in ...: result.append(x) return result
We can rewrite this code as a generator like so:
def foo(): for x in ...: yield x
Although now we have to write
list(foo()) if we explicitly want a list, this approach is still more flexible and efficient. Unlike the first version, we are no longer creating an entire list in memory every time. This allows us to use functions from the
itertools library to generate only the amount values we need via such functions as
For more information I suggest reading ‘Iterables vs. Iterators vs. Generators’ by Vincent Driessen, whose article is the source for this tip.