13.2. Generator Expression

  • Comprehensions executes instantly

  • Comprehensions are stored in the memory until end of a program

  • Comprehensions should be used when accessing values more than one

  • Generator Expressions are lazy evaluated

  • Generator Expressions are cleared once they are executed

  • Generator Expressions should be used when accessing value once (for example in the loop)

13.2.1. List Comprehension

  • Comprehensions executes instantly

  • Comprehensions will be in the memory until end of a program

  • Comprehensions - Using values more than one

Definition:

>>> data = (1,2,3,4)
>>> result = [x for x in data]

Type:

>>> type(result)
<class 'list'>

Result:

>>> result
[1, 2, 3, 4]

13.2.2. Generator Expression

  • Generators are lazy evaluated

  • Creates generator object and assign reference

  • Code is not executed instantly

  • Sometimes code is not executed at all!

  • Are cleared once they are executed

  • Generator will calculate next number for every loop iteration

  • Generator forgets previous number

  • Generator doesn't know the next number

  • It is used for one-time access to values (for example in the loop iterator)

Definition:

>>> data = (1,2,3,4)
>>> result = (x for x in data)

Type:

>>> type(result)
<class 'generator'>

Result:

>>> next(result)
1
>>>
>>> next(result)
2
>>>
>>> next(result)
3
>>>
>>> next(result)
4
>>>
>>> next(result)
Traceback (most recent call last):
StopIteration

13.2.3. Comprehensions or Generator Expression

  • If you need values evaluated instantly, there is no point in using generators

Comprehensions vs. Generator Expression:

>>> data = [x for x in range(0,10)]
>>> print(data)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> data = (x for x in range(0,10))
>>> print(data)  
<generator object <genexpr> at 0x...>

Comprehension:

>>> data = [x for x in range(0,10)]
>>>
>>> for x in data:  
...     print(x, end=' ')
...     if x == 3:
...         break
0 1 2 3
>>>
>>> for x in data:  
...     print(x, end=' ')
...     if x == 6:
...         break
0 1 2 3 4 5 6
>>>
>>> print(list(data))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> print(list(data))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Generator Expressions:

>>> data = (x for x in range(0,10))
>>>
>>> for x in data:  
...     print(x, end=' ')
...     if x == 3:
...         break
0 1 2 3
>>>
>>> for x in data:  
...     print(x, end=' ')
...     if x == 6:
...         break
4 5 6
>>>
>>> print(list(data))
[7, 8, 9]
>>>
>>> print(list(data))
[]

13.2.4. Why Round Brackets?

  • Round brackets does not produce tuples (commas does)

  • Round brackets bounds context

>>> a = [1+2] * 3
>>> b = (1+2) * 3
>>> c = (1+2,) * 3
>>>
>>> a
[3, 3, 3]
>>>
>>> b
9
>>> c
(3, 3, 3)

Adding round brackets will not change the data type. They are just a boundary for some context.

13.2.5. Case Study

>>> data = (1, 2, 3, 4)
>>>
>>> result = (x for x in data)          # generator expression
>>> result = [x for x in data]          # list comprehension
>>> result = {x for x in data}          # set comprehension
>>> result = {x:x for x in data}        # dict comprehension
>>>
>>> result = tuple(x for x in data)     # tuple comprehension
>>> result = list(x for x in data)      # list comprehension
>>> result = set(x for x in data)       # set comprehension
>>> result = dict((x,x) for x in data)  # dict comprehension
>>>
>>> result = bool(x for x in data)
>>> result = str(x for x in data)
>>> result = all(x for x in data)
>>> result = any(x for x in data)
>>> result = sum(x for x in data)
>>> result = min(x for x in data)
>>> result = max(x for x in data)
>>> result = hash(x for x in data)
>>> result = callable(x for x in data)
>>>
>>> result = len(x for x in data)
Traceback (most recent call last):
TypeError: object of type 'generator' has no len()