Iterator Objects¶
Python provides two general-purpose iterator objects. The first, a sequenceiterator, works with an arbitrary sequence supporting the__getitem__()method. The second works with a callable object and a sentinel value, callingthe callable for each item in the sequence, and ending the iteration when thesentinel value is returned.
- PyTypeObjectPySeqIter_Type¶
- Part of theStable ABI.
Type object for iterator objects returned by
PySeqIter_New()and theone-argument form of theiter()built-in function for built-in sequencetypes.
- intPySeqIter_Check(PyObject*op)¶
Return true if the type ofop is
PySeqIter_Type. This functionalways succeeds.
- PyObject*PySeqIter_New(PyObject*seq)¶
- Return value: New reference. Part of theStable ABI.
Return an iterator that works with a general sequence object,seq. Theiteration ends when the sequence raises
IndexErrorfor the subscriptingoperation.
- PyTypeObjectPyCallIter_Type¶
- Part of theStable ABI.
Type object for iterator objects returned by
PyCallIter_New()and thetwo-argument form of theiter()built-in function.
- intPyCallIter_Check(PyObject*op)¶
Return true if the type ofop is
PyCallIter_Type. Thisfunction always succeeds.
- PyObject*PyCallIter_New(PyObject*callable,PyObject*sentinel)¶
- Return value: New reference. Part of theStable ABI.
Return a new iterator. The first parameter,callable, can be any Pythoncallable object that can be called with no parameters; each call to it shouldreturn the next item in the iteration. Whencallable returns a value equal tosentinel, the iteration will be terminated.
Range Objects¶
- PyTypeObjectPyRange_Type¶
- Part of theStable ABI.
The type object for
rangeobjects.
Builtin Iterator Types¶
These are built-in iteration types that are included in Python’s C API, butprovide no additional functions. They are here for completeness.
C type | Python type |
|---|---|
| |
| |
| |
| |
|
Other Iterator Objects¶
- PyTypeObjectPyByteArrayIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPyBytesIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPyListIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPyListRevIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPySetIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPyTupleIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPyRangeIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPyLongRangeIter_Type¶
- Part of theStable ABI.
- PyTypeObjectPyDictIterKey_Type¶
- Part of theStable ABI.
- PyTypeObjectPyDictRevIterKey_Type¶
- Part of theStable ABI since version 3.8.
- PyTypeObjectPyDictIterValue_Type¶
- Part of theStable ABI.
- PyTypeObjectPyDictRevIterValue_Type¶
- Part of theStable ABI since version 3.8.
- PyTypeObjectPyDictIterItem_Type¶
- Part of theStable ABI.
- PyTypeObjectPyDictRevIterItem_Type¶
- Part of theStable ABI since version 3.8.
- PyTypeObjectPyODictIter_Type¶
Type objects for iterators of various built-in objects.
Do not create instances of these directly; prefer calling
PyObject_GetIter()instead.Note that there is no guarantee that a given built-in type uses a given iteratortype. For example, iterating over
rangewill use one of two iteratortypes depending on the size of the range. Other types may start using asimilar scheme in the future, without warning.