Introduction to Generators

Lesson Introduction

Welcome! Today, we will learn about an exciting Python feature called generators. Generators help us write code that uses memory efficiently, which is crucial when handling large amounts of data. By the end of this lesson, you'll understand what generators are and how to create and use them in Python. You’ll learn how they can make your code more efficient, especially when working with large datasets.

Why Understand Generators?

Imagine you have a vast book. You will read one sentence at a time rather than holding the whole book in your head at once. Generators in Python work similarly: they let us handle large data collections one item at a time.

Concept of Generators

Generators are special Python functions that return one item at a time, which helps process large datasets efficiently. Unlike normal functions that return a list of items and hold all items in memory at once, generators yield items one by one, using less memory and processing time.

Here's a quick comparison:

  • A normal function returns all items at once (like a big bag of candies).
  • A generator yields one item at a time (giving one candy at a time).

Example: Using a Normal Function to Return a List

Let's look at a normal function that returns a list of numbers from 1 to 5.

def normal_function():
    return [1, 2, 3, 4, 5]

def main():
    numbers = normal_function()
    for number in numbers:
        print(number)  # Output: 1, 2, 3, 4, 5

if __name__ == "__main__":
    main()

This function returns a list of five numbers. Now, let's see how generators do it differently.

Example of a Simple Generator

Here is a simple_generator function:

def simple_generator():
    yield 1
    yield 2
    yield 3
    yield 4
    yield 5

The simple_generator function uses the yield keyword. Unlike return, which exits the function, yield pauses the function and saves its state.

The yield keyword is what makes a function a generator. Here’s what happens when a function containing yield is called:

  1. Creates a Generator Object: Instead of running the function, it returns a generator object.
  2. Pausing and Resuming: When the generator's __next__() method is called (e.g., via next() function or a for loop), the function runs until it hits yield.
  3. Saves State and Returns Value: The function pauses at yield, saves its current state (local variables, execution point), and returns the yielded value.
  4. Resumes from Last State: When __next__() is called again, it resumes right after the last yield statement, with all its variables intact.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal