Detailed explanation of Python iterators and iterable objects
In Python development, "traversal" is one of the most common operations - whether it is scanning a list, viewing dictionary key-value pairs, or reading a file line by line, we are accustomed to writing afor ... in .... But have you ever been curious: **Why can these things be put in?forcycle? Why some objects can be used directlynext()Get elements? ** The answer lies in two core concepts: "Iterable" and "Iterator".
Iterable object (Iterable)
Simply put, an iterable object is one that “can beforThe object of loop traversal"**. It itself does not have the ability to directly produce the "next value", but it can "convert" a tool that can produce elements one by one - that is, an iterator.
Common iterable objects in Python can be divided into two categories:
- Basic Collection Class:
list、tuple、dict、set、str、byteswait - Objects with delay characteristics: generator, file object,
zip()/map()/filter()return value, etc.
How to determine whether it is iterable?
useisinstance()In conjunction with the standard librarycollections.abc.Iterable:
Iterator
Iterators are an "advanced version" of iterable objects - they can not only enterforCycle, ** can still benext()Actively called, taking out elements one by one until exhausted and thrownStopIterationabnormal**. It can be understood as "a tool that holds data flow control."
How to determine whether it is an iterator?
Also useisinstance(), this time withcollections.abc.Iterator:
Get iterator from iterable object
Use built-in functionsiter()You can easily convert:
Note: Iterators are very "lazy" and only work when you call
next()It will calculate and return the next value only when it is running, which provides the possibility to handle large amounts of data or infinite sequences.
Iterators vs. Iterable objects: What’s the difference?
Sometimes it’s easy to get confused by just reading the text. Let’s use a comparison table and actual scenarios to explain the differences thoroughly:
Let’s take the most intuitive example of infinite natural number sequence:
- Use lists? impossible--
[0, 1, 2, …]Sooner or later the memory will be filled up. - Implemented using iterators? Easily (I will customize one for you later).
Let’s look at another example of memory usage comparison (under CPython, the memory overhead of integers varies due to factors such as small integer pools, but here we mainly look at the trend):
You can even just create the iterator immediately withnext()Get values on demand without waiting for them to be generated.
forThe “true face” of cycles
every line you writefor x in xxx, the bottom layer of Python will automatically complete these three things:
- Call
iter(xxx)Get the corresponding iterator object - Loop call
next(迭代器), and assign the result tox - Once captured
StopIterationException, exit the loop gracefully
For example, this simple code:
Python is actually equivalent to executing the following logic internally:
Do you feel nowforIs the cycle less "mysterious"?
Practical Development Suggestions
1. When dealing with big data/infinite sequences, give priority to iterators or generators
When you have to process files with hundreds of thousands or millions of lines, or want to traverse infinite sequences, please avoid list comprehensions and use generator expressions or custom iterators instead, otherwise the memory will soon be exhausted.
2. Custom iterator, only two methods are needed
If you want to write an iterator class yourself (for example, to implement the infinite natural numbers just mentioned), you must implement two special methods:
__iter__():Must return the iterator itself (self), this is the key to keeping the protocol unified between iterators and iterable objects__next__():Responsible for "removing the current element, moving to the next element, or throwing at the endStopIteration”
Go directly to the code to implement an infinite natural number iterator starting from the specified starting point:
If your sequence has an end, just add__next__()Just add the termination condition:
3. Iterators are "disposable"!
This feature is very easy to get into trouble: The iterator will be empty after traversing it once, and then use it againnext()orforNone of the loops get any value.
Tips: If you need to traverse repeatedly, you should get a new iterator each time, or directly save the original iterable object (such as a list) and call it again when needed
iter()。
Summarize
A thorough understanding of iterators and iterable objects is an important cornerstone of writing efficient, Pythonic, memory-friendly code. Let us consolidate it in one sentence:
- Iterable is the "raw material/container" that "provides" iterators
- Iterator is a "tool/worker" that holds the logic of lazy calculation and produces the next value on demand
forLoops are just syntactic sugar for the Iterator pattern- Custom iterator needs to be implemented
__iter__()(returns itself) and__next__()
Next time you write traversal code, you might as well think a little more: Is it better to use a list here, or is it more appropriate to use a generator or iterator? Your programs may become more elegant and robust as a result.

