Build a generator
In previous exercises, you've dealt mainly with writing generator expressions, which uses comprehension syntax. Being able to use comprehension syntax for generator expressions made your work so much easier!
Now, recall from the video that not only are there generator expressions, there are generator functions as well. Generator functions are functions that, like generator expressions, yield a series of values, instead of returning a single value. A generator function is defined as you do a regular function, but whenever it generates a value, it uses the keyword yield
instead of return
.
In this exercise, you will create a generator function with a similar mechanism as the generator expression you defined in the previous exercise:
lengths = (len(person) for person in lannister)
This exercise is part of the course
Python Toolbox
Exercise instructions
- Complete the function header for the function
get_lengths()
that has a single parameter,input_list
. - In the
for
loop in the function definition,yield
the length of the strings ininput_list
. - Complete the iterable part of the
for
loop for printing the values generated by theget_lengths()
generator function. Supply the call toget_lengths()
, passing in the listlannister
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a list of strings
lannister = ['cersei', 'jaime', 'tywin', 'tyrion', 'joffrey']
# Define generator function get_lengths
def ____:
"""Generator function that yields the
length of the strings in input_list."""
# Yield the length of a string
for person in input_list:
____
# Print the values generated by get_lengths()
for value in ____:
print(value)