Pages

Saturday, December 26, 2015

Python Generator Expression

Generator Expressions are generator version of list comprehensions. They look like list comprehensions, but return a generator back instead of a list. One drawback with the list comprehensions is that values are calculated all at once regardless of whether the values are needed at that time or not. This sometimes can consume amount of computer memory. Thus the generator expressions were suggested.

Generator expressions are like list comprehensions; the only difference is that the square brackets in list comprehensions are replaced by circular brackets that return. A simple list comprehension for getting the square root is as follows

>>> squ = [ i*2 for i in range(5)]
>>> print squ
[0, 2, 4, 6, 8]

Now the same thing can be generated using the Generator Comprehension are,

>>> squ = (i*2 for i in range(5))
>>> print squ
<generator object <genexpr> at 0x8734870>

Now when we print the squ, we don’t see the elements rather we see the Object type. Now if want to see the element we need to use the for loop like,

>>> for s in squ:
...     print s
...
0
2
4
6
8

Now lets define a sample Class that provides us the Generator approach,

#!/software/python/2.7

def Reverse(data):
    for index in range(len(data)-1,-1,-1):
        yield data[index]

def Main():
    ref = Reverse("This is Jagadish")
    for c in ref:
        print c

if __name__  == "__main__":
    Main()

The same thing can be written using the generator expression approach as,

data ="master is good"
print(list(data[i] for i in range(len(data)-1,-1,-1)))

So where does these Generators are being used,

So what does this generator expression provide over list comprehensions -
Iterating over the generator expression or the list comprehension will do the same thing. However, the list comprehension will create the entire list in memory first while the generator expression will create the items on the fly, so you are able to use it for very large (and also infinite!) sequences

Use list comprehensions when the result needs to be iterated over multiple times, or where speed is paramount. Use generator expressions where the range is large or infinite.


The benefit of a generator expression is that it uses less memory since it doesn't build the whole list at once. 

No comments :

Post a Comment