Here in this post, we’ll see what is generator expression and most important we will compare List Comprehension and Generator Expression to know the difference between them and which one we should use and why.
Let’s take List comprehension and generator expression one by one. First, let’s recall what we know about list comprehension. If you don’t have any idea about list comprehension, check out our using List and Dictionary Comprehensions in Python.
The basic syntax of List Comprehension is
Let’s take an example of List Comprehension:
[2 * num for num in range(5)] Output- [0, 2, 4, 6, 8]
Let’s break the above code into three components to match it with the basic syntax of the List comprehension:
- Output Expression = 2 * num
- Iterator = for loop
- Iterable = range(10).
To generate the Generator expression from the above code we just have to change the square bracket into round brackets or parenthesis ().
The basic syntax of Generator Expression and list comprehension is similar with the only difference of the type of brackets we use:
Let’s take an example of Generator Expression:
(2 * num for num in range(5)) Output- <generator object at 0x0000019768461CF0>
So from the above code execution, we can clearly see that generator expression doesn’t provide us with the output but it returned a generator object on which we can iterate and print the results.
Conditionals on Generator Expression:
The conditionals on generator expression work the same way they work on list comprehensions.
Conditionals on Iterables:
In the below code we have added conditional on the iterable. Here in the below expression, we added if statement on iterable which will check whether the number in the iterable is completely divisible by 2 and giving a remainder as 0. Output expression will provide values of those which satisfy the if condition.
even_num = (num for num in range(10) if num % 2 == 0) print(list(even_num) Output- [0, 2, 4, 6, 8]
Conditionals on Output Expression:
Similarly, as above, this time the if statement is added on output expression and the values which satisfy the criteria otherwise the default value in else will be printed (in this case 0 is printed if the condition in if statement is false)
even_num = (num if num % 2 == 0 else 0 for num in range(10)) print(list(even_num)) Output- [0, 0, 2, 0, 4, 0, 6, 0, 8, 0]
Printing values from Generator Expression:
There are three basic ways by which you can print the values from the generator objects:
- Using for loop:
result = (num for num in range(5)) print(result) <generator object <genexpr> at 0x0000019768461DE0> for num in result: print(num) Output- 0 1 2 3 4
- By passing the generator object into the list() function. we have seen this above in this post:
result = (num for num in range(5)) print(list(result)) [0, 1, 2, 3, 4]
- By iterating over Iterators using next():
We can easily print the values which generator object have saved by iterating over Iterators using next() function as shown below:
result = (num for num in range(5)) print(next(result)) 0 print(next(result)) 1 print(next(result)) 2 print(next(result)) 3 print(next(result)) 4
List Comprehension and Generator Expression:
Below is a table to show a quick comparison between List Comprehension and Generator Expression in Python:
|List Comprehension||Generator Expression|
|For list comprehension, we use square brackets ||For generator expression, we use parenthesis or round bracket ()|
|Returns a list||Returns a generator object|
|It can be iterated over||It can be iterated over|
|Will use memory as it will save the values||Cannot use the memory as it is an object on which we can iterate over to produce the elements|
|Values can be easily printed in a simple way using the print function||Values can be easily printed by using for loop or passing it to list as shown above.|
To know more about list comprehension and also about dictionary comprehension, check out our post – Using List and Dictionary Comprehensions in Python.
Hope you gain some idea from this post regarding the differences and implementation of List Comprehension and Generator Expression. You can view the official Python 3 Documentation for more detailed information. If you want to learn more like installing python and basic concepts, you can check here.