Day 41: Write Generator Function

What is generator function?

– It produces generator object when it called.
– It defines like general function using def keyword.
– Its return value is using yield keyword. It yields a sequence of values instead of a single value.

How to build a generator?

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. Let us look at the exercise in DataCamp’s tutorial which walk me through how to write generator function in Python.

The instructions as below:
1. Complete the function header for the function get_lengths() that has a single parameter, input_list.
2. In the for loop in the function definition, yield the length of the strings in input_list.
3. Complete the iterable part of the for loop for printing the values generated by the get_lengths() generator function. Supply the call to get_lengths(), passing in the list lannister.

The code as below:

# Create a list of strings
lannister = ['cersei', 'jaime', 'tywin', 'tyrion', 'joffrey']

# Define generator function get_lengths
def get_lengths(input_list):
    """Generator function that yields the
    length of the strings in input_list."""

    # Yield the length of a string
    for person in input_list:
        yield len(person)

# Print the values generated by get_lengths()
for value in get_lengths(lannister):
    print(value)

#Output:
#6
#5
#5
#6
#7

Summary of the day:

  • Generator function.
  • Keyword: yield.

Day 41: Introduction to Generator in Python

Generator has some connections with list comprehension. Still remember, the list comprehension is using square brackets [].

Generator expression
Instead of using square brackets, it uses normal brackets. When I execute the code, it creates generator object.

The syntax:

( output expression for iterator variable in iterable )

What is this generator?
It is same as list comprehension except it does not store the list in the memory.

List comprehensions vs generators vs dict comprehensions

So now, there are three things to remember, their differences,
– List comprehension returns a list.
– Dict comprehension returns a dictionary.
– Generator returns generator object.

Generator is good to use when we want to generate a large volume of data, example, a range of 10*10000000. List comprehension may cause the server out of memory. However, generator will be able to do so because it has not yet generated the entire list.

List comprehensions and generator expressions look very similar in their syntax, except for the use of parentheses () in generator expressions and brackets [] in list comprehensions. Both can be iterated over.

Below is an example from DataCamp whereby, it creates generator object using bracket () and combines with next() method which I learned from the iterators to iterate each element and print the first 5 values out of the range 0 to 50. The remaining values are then print out using the for loop statement. Let see the codes below:

# Create generator object: result
result = (num for num in range(0,31))

# Print the first 5 values
print(next(result))
print(next(result))
print(next(result))
print(next(result))
print(next(result))

# Print the rest of the values
for value in result:
    print(value)

Next, what we can apply in list comprehension such as conditionals in list comprehension, it applies to generator expression.

Changing the output in generator expressions
It works similarly as the list comprehension, we are able to add to the output expression of a generator expression. Example as below:

# Create a list of strings: lannister
lannister = ['cersei', 'jaime', 'tywin', 'tyrion', 'joffrey']

# Create a generator object: lengths
lengths = (len(person) for person in lannister)

# Iterate over and print the values in lengths
for value in lengths:
    print(value)

It generates lengths of each string in the list. Then, uses the for loop statement to print out the value in the generator object.

Lastly, generator function which produces generator object when it called. I will cover generator function in my next entry.

Summary of the day:

  • Generators in Python.
  • List comprehensions vs generators.
  • Conditionals in generator expressions.