Master Python Iterables, Iterators, and Generators: A Complete Guide
This article explains the concepts and differences between Python iterables, iterators, and generators, shows how to create custom iterable and iterator classes, demonstrates the use of __iter__ and __next__ magic methods, and provides practical generator examples for efficient lazy evaluation.
In Python you often encounter iterables, iterators, and generators; this guide shows how to obtain each, how they differ, and how they relate.
Conceptually, the relationship is: iterable > iterator > generator.
Iterable
An iterable (Iterable) is a container that holds N elements and can be iterated over. In Python, any object that can be used in a for loop is an iterable, such as list, tuple, range, str, bytes, bytearray, set, dict, etc.
Custom Iterable
To make a custom class iterable, implement the __iter__ magic method.
class MyIterable:
def __str__(self):
return "我还不是一个可迭代对象"
mi = MyIterable()
print(mi)
for i in mi:
print(i) # raises TypeError: 'MyIterable' object is not iterableAdding __iter__:
class MyIterable:
def __iter__(self):
print('iter~~~~~~~')
# should return an iterator, but returns None here
mi = MyIterable()
print(mi)
for i in mi:
print(i) # TypeError: iter() returned non-iterator of type 'NoneType'Since __iter__ must return an iterator, the above fails.
Iterator
An iterator is a special iterable that can produce its next element via the built‑in next() function. After all elements are consumed, next() raises StopIteration. An iterator cannot be iterated again once exhausted.
It is a special iterable that always supports iteration.
Use next() to retrieve the next element.
When exhausted, next() raises StopIteration.
After exhaustion, it cannot be iterated again.
Lists are iterables but not iterators. Convert an iterable to an iterator with iter() or by wrapping it in a generator.
a = [1, 2]
# print(next(a)) # TypeError: 'list' object is not an iterator
b = iter(a) # built‑in iter()
print(next(b))
print(next(b))
print(next(b)) # StopIterationMany built‑in functions return iterators, e.g., enumerate:
a = [1, 2]
c = enumerate(a)
print(next(c))
print(next(c))
for x in c:
print(x) # no more elements
print('=' * 30)
print(next(c)) # StopIterationCustom Iterator
To make a class act as an iterator, implement both __iter__ (returning self) and __next__:
class MyIterable:
def __init__(self):
self.items = [1,2,3,4,5]
self.count = 0
def __iter__(self):
print('iter~~~~~~~')
return iter(self.items)
def __next__(self):
print('next ~~~~')
try:
n = self.items[self.count]
self.count += 1
return n
except IndexError:
raise StopIteration
mi = MyIterable()
print(mi)
print(next(mi))
print(next(mi))
print(next(mi))
print(next(mi))
print(next(mi))
print(next(mi)) # StopIterationImproving the class so it is both iterable and its own iterator:
class MyIterable:
def __init__(self):
self.items = [1,2,3,4,5]
self.count = 0
def __iter__(self):
print('iter~~~~~~~')
return self # self implements __next__
def __next__(self):
print('next ~~~~')
try:
n = self.items[self.count]
self.count += 1
return n
except Exception:
raise StopIteration
mi = MyIterable()
print(mi)
print(next(mi)) # works as iterator
for i in mi:
print(i) # iterable again
print('-' * 30)
for i in mi:
print(i) # cannot iterate again after exhaustion
print('-' * 30)
print(next(mi)) # StopIterationGenerator
Python provides two ways to obtain a generator object, which is a special kind of iterator:
Generator expression
Generator function
Generators are lazy‑evaluation objects; they produce items on demand.
g1 = (i for i in range(5)) # generator expression
print(g1) # <generator object ...>
print(next(g1))
print(next(g1))
print('=' * 30)
def counter(): # generator function
for i in range(5, 10):
yield i
g2 = counter()
print(g2)
print(next(g2))
print(next(g2))Summary
Since Python 3, using iterators (and generators) is recommended wherever possible because they compute lazily, reducing memory and CPU pressure.
Generator expressions give a quick way to obtain an iterator; for more complex logic, use a generator function; for even more control, implement a custom class with __iter__ and __next__.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
