Python provides two general-purpose iterator objects. The first, a sequence iterator, works with an arbitrary sequence supporting the
method. The second works with a callable object and a sentinel value, calling the callable for each item in the sequence, and ending the iteration when the sentinel value is returned.
PySeqIter_Type
Part of the
.Type object for iterator objects returned by
and the one-argument form of the
built-in function for built-in sequence types.
intPySeqIter_Check(
*op)
Return true if the type of op is
. This function always succeeds.
*PySeqIter_New(
*seq)
Return value: New reference. Part of the
.Return an iterator that works with a general sequence object, seq. The iteration ends when the sequence raises
for the subscripting operation.
PyCallIter_Type
Part of the
.Type object for iterator objects returned by
and the two-argument form of the
built-in function.
intPyCallIter_Check(
*op)
Return true if the type of op is
. This function always succeeds.
*PyCallIter_New(
*callable,
*sentinel)
Return value: New reference. Part of the
.Return a new iterator. The first parameter, callable, can be any Python callable object that can be called with no parameters; each call to it should return the next item in the iteration. When callable returns a value equal to sentinel, the iteration will be terminated.
Range Objects
PyRange_Type
Part of the
.The type object for
objects.
intPyRange_Check(
*o)
Return true if the object o is an instance of a
object. This function always succeeds.
Builtin Iterator Types
These are built-in iteration types that are included in Python’s C API, but provide no additional functions. They are here for completeness.
C type
Python type
PyEnum_Type
Part of the
.
PyFilter_Type
Part of the
.
PyMap_Type
Part of the
.
PyReversed_Type
Part of the
.
PyZip_Type
Part of the
.
Other Iterator Objects
PyByteArrayIter_Type
Part of the
.
PyBytesIter_Type
Part of the
.
PyListIter_Type
Part of the
.
PyListRevIter_Type
Part of the
.
PySetIter_Type
Part of the
.
PyTupleIter_Type
Part of the
.
PyRangeIter_Type
Part of the
.
PyLongRangeIter_Type
Part of the
.
PyDictIterKey_Type
Part of the
.
PyDictRevIterKey_Type
Part of the
since version 3.8.
PyDictIterValue_Type
Part of the
.
PyDictRevIterValue_Type
Part of the
since version 3.8.
PyDictIterItem_Type
Part of the
.
PyDictRevIterItem_Type
Part of the
since version 3.8.
PyODictIter_Type
Type objects for iterators of various built-in objects.
Do not create instances of these directly; prefer calling
instead.
Note that there is no guarantee that a given built-in type uses a given iterator type. For example, iterating over
will use one of two iterator types depending on the size of the range. Other types may start using a similar scheme in the future, without warning.