0Pricing
Learn AI with Python · Lesson

Iterators and Iterables

Explore the iterator protocol for creating custom iteration behavior.

Iterators and Iterables is a free Learn AI with Python lesson on CoddyKit — lesson 5 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Iterators and Iterables

Iterators and iterables are fundamental concepts in Python that allow you to work with sequences of data. They are the foundation of loops, comprehensions, and generators.

In this lesson, you’ll learn how iterators and iterables work and how to create your own iterators.

Iterators and Iterables — illustration 1

2

What Is an Iterable?

An iterable is any object in Python that can be looped over using a for loop. Examples include lists, tuples, sets, and dictionaries.

# Example of an iterable
my_list = [1, 2, 3]
for item in my_list:
    print(item)  # Outputs: 1, 2, 3

3

What Is an Iterator?

An iterator is an object that represents a stream of data. It implements the __iter__() and __next__() methods.

# Example of an iterator
my_list = [1, 2, 3]
iterator = iter(my_list)
print(next(iterator))  # Outputs: 1
print(next(iterator))  # Outputs: 2
print(next(iterator))  # Outputs: 3

4

The Difference Between Iterables and Iterators

Iterables can be looped over (e.g., lists), while iterators produce one item at a time using the next() method. An iterable becomes an iterator when passed to the iter() function.

5

Creating Custom Iterators

You can create custom iterators by defining a class with __iter__() and __next__() methods:

# Custom iterator example
class Counter:
    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
        self.current += 1
        return self.current - 1

counter = Counter(1, 5)
for number in counter:
    print(number)  # Outputs: 1, 2, 3, 4, 5

6

Using the iter() Function

The iter() function returns an iterator object from an iterable:

# Using iter() on a list
my_list = [1, 2, 3]
iterator = iter(my_list)
print(next(iterator))  # Outputs: 1

7

Handling StopIteration

When an iterator is exhausted, it raises a StopIteration exception. This is how Python signals the end of a sequence.

# Handling StopIteration
my_list = [1, 2, 3]
iterator = iter(my_list)

while True:
    try:
        print(next(iterator))
    except StopIteration:
        break

8

Iterators vs. Generators

Generators are a simpler way to create iterators. They automatically implement the __iter__() and __next__() methods.

# Example of a generator as an iterator
def my_generator():
    yield 1
    yield 2
    yield 3

for value in my_generator():
    print(value)  # Outputs: 1, 2, 3

9

10

Common Mistakes with Iterators

Here are some mistakes to avoid:

  • Forgetting to implement __iter__() and __next__() in custom iterators.
  • Not handling StopIteration exceptions properly.
  • Confusing iterators with iterables.

11

What Did We Learn?

In this lesson, you learned:

  • The difference between iterables and iterators.
  • How to use __iter__() and __next__() to create custom iterators.
  • How to use the iter() function and handle StopIteration exceptions.
  • How generators simplify the creation of iterators.

Great job! Let’s move to the next topic.

Iterators and Iterables — illustration 11

Frequently asked questions

Is the “Iterators and Iterables” lesson free?

Yes — the full text of “Iterators and Iterables” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Iterators and Iterables”?

Explore the iterator protocol for creating custom iteration behavior. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 5 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Iterators and Iterables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Decorators
  2. Generators
  3. Context Managers (with Statements)
  4. Comprehensions (List, Set, and Dictionary)
  5. Iterators and Iterables
← Back to Learn AI with Python