A Comparative Analysis between Generator expressions and List Comprehensions

Guru Pattnayak
2 min readDec 17, 2020

In Python Framework we can either use generators or list comprehensions to solve many coding requirements, however, a very simplistic classification can be made when to use a generator expression or when to use list comprehensions.

If our focus is to optimize memory or we need our code to consume less memory to store its intermediate results we can use generator expressions instead of list comprehensions(Evidence is clearly visible in the below with explanation).

import sys

list_square=[i**2 for i in range(5)]

gene_sq=(i*i for in in range(5))

print(sys.getsizeof(list_square)) — — This will give the output as 120

print(sys.getsizeof(gene_sq)) — — This will give the output as 112

Reason — Generators use the lazy iterator unlike the normal list which do not store their contents in memory as conventional list object does.

Now, List comprehension has an advantage over generator expressions when computation speed is taken into consideration.

To summarize list comprehensions are faster to evaluate than generator expressions we will see by using a sum function on both List and generator object respectively(Below is the evidence via statistics).

import cProfile

cProfile.run(‘sum([i ** 2 for i in range(10000)])’)

The Output is below:-

5 function calls in 0.007 seconds

import cProfile

cProfile.run(‘sum((i * *2 for i in range(10000)))’)

The Output is below:-

10005 function calls in 0.010 seconds

Clearly we can observe the computation time taken for sum function to complete on a list is 0.007 seconds and the time taken to complete the same sum function on a generator expression is 0.010 seconds.

Thus, if memory is a factor then generator expressions are useful, however if performance is a factor then list comprehensions are a better choice respectively.

--

--

Guru Pattnayak
0 Followers

Passionate to Learn and Explore new technologies