What is a generator and how to create one?

Practicing Coding Interview Questions in Python

Kirill Smirnov

Data Science Consultant, Altran

Definition

Generator - a special iterable object created by a function having a yield keyword in its body.

def func():
    # Return a value from super complex calculations
    return 0
result = func()
print(result)
0
Practicing Coding Interview Questions in Python

Definition

Generator - a special iterable object created by a function having a yield keyword in its body.

def func():
    # Yield a value from super complex calculations
    yield 0
result = func()
print(result)
<generator object result at 0x105736e10>
Practicing Coding Interview Questions in Python

Generator as Iterable

def func():
    # Yield a value from super complex calculations
    yield 0

result = func()
for item in result:
    print(item)
0
Practicing Coding Interview Questions in Python

More yields!

def func():
    yield 0
    yield 1
    yield 2
result = func()
for item in result:
    print(item)
0
1
2
Practicing Coding Interview Questions in Python

Yield in a loop

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
for item in result:
    print(item)
0
2
4
Practicing Coding Interview Questions in Python

Converting a generator to a list

def func(n):
    for i in range(0, n):
        yield 2*i

result = func(5)
list(result)
[0, 2, 4, 6, 8]
Practicing Coding Interview Questions in Python

Generator as Iterator

Generator is an Iterable AND an Iterator

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
next(result)
0
next(result)
2
next(result)
4
next(result)
StopIteration
Practicing Coding Interview Questions in Python

Generators are expendable

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
for item in result:
    print(item)
0
2
4
for item in result:
    print(item)
# nothing
result = func(3)
for item in result:
    print(item)
0
2
4
Practicing Coding Interview Questions in Python

Generators are expendable

def func(n):
    for i in range(0, n):
        yield 2*i
result = func(3)
list(result)
[0, 2, 4]
list(result)
[]
result = func(3)
list(result)
[0, 2, 4]
Practicing Coding Interview Questions in Python

Generator comprehension

result = [2*i for i in range(0, 3)]
print(result)
[0, 2, 4]
result = (2*i for i in range(0, 3))
print(result)
<generator object result at 0x105736e10>
Practicing Coding Interview Questions in Python

Traversal

result = (2*i for i in range(0, 3))
for item in result:
    print(item)
0
2
4
next(result)
StopIteration
Practicing Coding Interview Questions in Python

Why generators?

  • simple way to create a custom iterable object

[1, 3, 2, 4, 3, 5]

def create_jump_sequence(n):
    for i in range(1, n-1):
        yield i
        yield i+2
jump_sequence = create_jump_sequence(5)
list(jump_sequence)
[1, 3, 2, 4, 3, 5]
Practicing Coding Interview Questions in Python

Why generators?

  • simple way to create a custom iterable object
  • lazy initialization

[1, 3, 2, 4, 3, 5, 4, 6, 5, 7, ...]

def create_jump_sequence(n):
    for i in range(1, n-1):
        yield i
        yield i+2
jump_sequence = create_jump_sequence(500)
next(jump_sequence)
1
Practicing Coding Interview Questions in Python

Why generators?

  • simple way to create a custom iterable object
  • lazy initialization
  • possibility to create infinite iterable objects
def create_inf_generator():
    while True:
        yield 'I am infinite!'
inf_generator = create_inf_generator()
next(inf_generator)
I am infinite
Practicing Coding Interview Questions in Python

Let's practice!

Practicing Coding Interview Questions in Python

Preparing Video For Download...