Lesson 18: Iterators and Generators in Python

🎯 Lesson Objective

To understand how iterators and generators work in Python for efficient data traversal and memory management.


🧩 1. What Is an Iterator?

An iterator is an object that can be iterated (looped) over.
It implements two methods:

  • __iter__() β†’ returns the iterator object itself
  • __next__() β†’ returns the next value

βœ… Example: Using Iterator on a List

numbers = [1, 2, 3, 4]
it = iter(numbers)

print(next(it))  # 1
print(next(it))  # 2
print(next(it))  # 3
print(next(it))  # 4
# print(next(it))  # Raises StopIteration

πŸ”„ 2. Iterating Using a Loop

numbers = [5, 10, 15]
for num in numbers:
    print(num)

Output:

5
10
15

Python’s for loop automatically uses iterators.


🧰 3. Creating a Custom Iterator

class MyRange:
    def __init__(self, start, end):
        self.current = start
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.current > self.end:
            raise StopIteration
        val = self.current
        self.current += 1
        return val

nums = MyRange(1, 5)
for n in nums:
    print(n)

Output:

1
2
3
4
5

🧱 4. What Is a Generator?

A generator is a special function that yields values one by one, saving memory for large sequences.

βœ… Example: Generator Function

def my_generator(n):
    for i in range(1, n+1):
        yield i

gen = my_generator(5)
for val in gen:
    print(val)

Output:

1
2
3
4
5

πŸ”„ 5. Generator Expressions

Similar to list comprehensions but use parentheses and generate values on the fly.

squares = (x**2 for x in range(1, 6))
for val in squares:
    print(val)

Output:

1
4
9
16
25

πŸ› οΈ 6. Advantages of Generators

  • Memory efficient β†’ generate items one at a time.
  • Can handle large datasets.
  • Useful for pipelines and streaming data.

βœ… Example: Fibonacci Generator

def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

for num in fibonacci(10):
    print(num)

Output:

0
1
1
2
3
5
8
13
21
34

🌍 7. Real-Life Example – Reading Large File

def read_file_line_by_line(filename):
    with open(filename, "r") as file:
        for line in file:
            yield line.strip()

for line in read_file_line_by_line("example.txt"):
    print(line)
  • Only one line is in memory at a time, efficient for large files.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *