Fundamentals 5 min read

Master Python Iterators and Generators: From Basics to Advanced Examples

This article explains Python iterators and generators, covering their core methods, custom iterator classes with __iter__ and __next__, handling StopIteration, and practical generator examples such as a Fibonacci sequence, all illustrated with clear code snippets.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Iterators and Generators: From Basics to Advanced Examples

Iterator

Iteration is one of Python's most powerful features, providing a way to access elements of a collection.

An iterator is an object that remembers its current position in the traversal.

It starts from the first element of a collection and proceeds forward until all elements have been visited; it cannot move backward.

Iterators have two fundamental methods: iter() and next().

Strings, lists, or tuples can be used to create iterators.

Example (Python 3.0+)

list = [1, 2, 3, 4]
it = iter(list)  # create iterator object
print(next(it))  # 1
print(next(it))  # 2

The iterator object can also be traversed with a regular for loop.

Example (Python 3.0+)

#!/usr/bin/python3
list = [1, 2, 3, 4]
it = iter(list)
for x in it:
    print(x, end=" ")
# Output: 1 2 3 4

Using next() directly:

#!/usr/bin/python3
import sys
list = [1, 2, 3, 4]
it = iter(list)
while True:
    try:
        print(next(it))
    except StopIteration:
        sys.exit()

Creating a custom iterator class requires implementing __iter__() and __next__() methods, and optionally __init__() for initialization.

class MyNumbers:
    def __iter__(self):
        self.a = 1
        return self
    def __next__(self):
        x = self.a
        self.a += 1
        return x

myclass = MyNumbers()
myiter = iter(myclass)
print(next(myiter))  # 1
print(next(myiter))  # 2
print(next(myiter))  # 3
print(next(myiter))  # 4

When the iterator reaches a predefined limit, it should raise StopIteration to end the iteration.

class MyNumbers:
    def __iter__(self):
        self.a = 1
        return self
    def __next__(self):
        if self.a <= 20:
            x = self.a
            self.a += 1
            return x
        else:
            raise StopIteration

myclass = MyNumbers()
for x in myclass:
    print(x)
# Prints numbers 1 through 20

Generator

In Python, a function that contains the yield keyword is called a generator. Generators return an iterator and can be paused and resumed, yielding values each time next() is called.

Example: Fibonacci sequence generator.

#!/usr/bin/python3
import sys
def fibonacci(n):
    a, b, counter = 0, 1, 0
    while True:
        if counter > n:
            return
        yield a
        a, b = b, a + b
        counter += 1

f = fibonacci(10)
while True:
    try:
        print(next(f), end=" ")
    except StopIteration:
        sys.exit()
# Output: 0 1 1 2 3 5 8 13 21 34 55
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTutorialYieldStopIteration
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.