Python List Comprehensions vs Generator Expressions

29 Jun, 2018
What is List Comprehension?It is an elegant way of defining and creating a list. List Comprehension allows us to create a list using for loop with lesser code. What normally takes 3-4 lines of code, can be compressed into just a single line.
Example:
# initializing the listlist = []  for i in range(11):    if i % 2 == 0:        list.append(i)  # print elementsprint(list)
Output:
 0 2 4 6 8 10
Now, the same output can be derived from just a single line of code.
list = [i for i in range(11) if i % 2 == 0]print(list)
Output:
 0 2 4 6 8 10
What are Generator Expressions?Generator Expressions are somewhat similar to list comprehensions, but the former doesn’t construct list object. Instead of creating a list and keeping the whole sequence in the memory, the generator generates the next element in demand.When a normal function with a return statement is called, it terminates whenever it gets a return statement. But a function with a yield statement saves the state of the function and can be picked up from the same state, next time the function is called.The Generator Expression allows us to create a generator without the yield keyword.
Syntax Difference: Parenthesis are used in place of square brackets.
# List Comprehensionlist_comprehension = [i for i in range(11) if i % 2 == 0]  print(list_comprehension)
Output:
 0 2 4 6 8 10
# Generator Expressiongenerator_expression = (i for i in range(11) if i % 2 == 0)  print(generator_expression)
Output:
<generator object  at 0x000001452B1EEC50>

In the above example, if we want to print the output for generator expressions, we can simply iterate it over generator object.
for i in generator_expression:    print(i, end=" ")
Output:
0 2 4 6 8 10
So what’s the difference between Generator Expressions and List Comprehensions?The generator yields one item at a time and generates item only when in demand. Whereas, in a list comprehension, Python reserves memory for the whole list. Thus we can say that the generator expressions are memory efficient than the lists.We can see this in the example below.
# import getsizeof from sys modulefrom sys import getsizeof  comp = [i for i in range(10000)]gen = (i for i in range(10000))  #gives size for list comprehensionx = getsizeof(comp) print("x = ", x)  #gives size for generator expressiony = getsizeof(gen) print("y = ", y)
Output:
x =  87624
y =  88

We just saw that generator expression are memory efficient. But, are they time efficient too? Let’s check this with an example.
#List Comprehension: import timeit  print(timeit.timeit('''list_com = [i for i in range(100) if i % 2 == 0]''', number=1000000))
Output:
8.118047142050102
#Generator Expression:import timeit  print(timeit.timeit('''gen_exp = (i for i in range(100) if i % 2 == 0)''', number=1000000))
Output:
0.7548244756850693
There is a remarkable difference in the execution time. Thus, generator expressions are faster than list comprehension and hence time efficient.
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Create a directory in Python