Python22 min read

Python Generators

Learn generators and yield: iterate lazily, save memory, and build efficient pipelines for large datasets.

Michael Brown
July 30, 2025
4.0k164

Generators help you produce values one at a time instead of creating a full list in memory.

  This matters when:
  - you process huge files
  - you handle many records from a database
  - you stream data (logs, events, sensors)
  
  ## Generator vs list (big idea)
  
  - List creates everything now (eager)
  - Generator creates one item when asked (lazy)
  
  ## Basic generator with yield
  
  ```python
  def count_up_to(n):
      count = 1
      while count <= n:
          yield count
          count += 1
  
  for num in count_up_to(5):
      print(num)
  ```
  
  Expected output:
  
  ```
  1
  2
  3
  4
  5
  ```
  
  ## Generator vs list example
  
  ```python
  def get_squares_list(n):
      return [i ** 2 for i in range(n)]
  
  def get_squares_gen(n):
      for i in range(n):
          yield i ** 2
  
  print(get_squares_list(5))
  ```
  
  Expected output:
  
  ```
  [0, 1, 4, 9, 16]
  ```
  
  ## Generator expression (like list comprehension but lazy)
  
  ```python
  squares_gen = (i ** 2 for i in range(5))
  print(list(squares_gen))
  ```
  
  Expected output:
  
  ```
  [0, 1, 4, 9, 16]
  ```
  
  ## Real-world example: reading a large file safely
  
  ```python
  def read_large_file(file_path):
      with open(file_path, "r") as file:
          for line in file:
              yield line.strip()
  
  for line in read_large_file("huge_data.txt"):
      print(line)
  ```
  
  ## Graph: lazy iteration
  
  ```mermaid
  flowchart LR
    A[Generator] --> B[Next value requested]
    B --> C[Yield one item]
    C --> D[Stop or continue]
    D --> B
  ```
  
  In the next lesson, you will learn context managers, which are about safe resource handling like files and database connections.
#Python#Intermediate#Generators