Iterators and Iterables
Explore the iterator protocol for creating custom iteration behavior.
Iterators and Iterables is a free Python Academy lesson on CoddyKit. This is 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 Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 5 lessons in total.
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.

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, 3What 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: 3The 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.
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, 5Using 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: 1Handling 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:
breakIterators 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, 3Common Mistakes with Iterators
Here are some mistakes to avoid:
- Forgetting to implement
__iter__()and__next__()in custom iterators. - Not handling
StopIterationexceptions properly. - Confusing iterators with iterables.
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 handleStopIterationexceptions. - How generators simplify the creation of iterators.
Great job! Let’s move to the next topic.

Learn Python with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 76
- Lessons
- 320
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. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 5 lessons in total.
What will I learn in “Iterators and Iterables”?
Explore the iterator protocol for creating custom iteration behavior. You practise Python Academy 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 Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 5 of 5.
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 Python Academy lesson?
Yes. Every Python Academy 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
- Decorators
- Generators
- Context Managers (with Statements)
- Comprehensions (List, Set, and Dictionary)
- Iterators and Iterables