Python Iterators: Difference between revisions
Line 40: | Line 40: | ||
=Removing an Element while Iterating= | =Removing an Element while Iterating= | ||
It does not seem that Python iterators can remove elements while iterating, similar to Java's <code>Iterator.remove()</code>. To achieve the same behavior | It does not seem that Python iterators can remove elements while iterating, similar to Java's <code>Iterator.remove()</code>. To achieve the same behavior, use a list and [[Python_Language_List#Remove_an_Element_while_Iterating|remove an element while iterating the list]]. | ||
=<tt>itertools</tt>= | =<tt>itertools</tt>= | ||
<font color=darkkhaki>To explore</font> | <font color=darkkhaki>To explore</font> |
Revision as of 22:00, 8 December 2022
External
Internal
TODO
TO PROCESS PyOOP "The Iterator Pattern" + "Iterators" + "The iterator protocol"
Overview
An iterator instance represents a stream of data.
The iterator instances are created from iterable objects with the built-in function iter()
:
l = ['a', 'b', 'c']
i = iter(l)
Once created, repeated invocations of the iterator's __next__()
method, or by passing it to the built-in function next()
, return successive items in the stream:
assert next(i) == 'a'
assert next(i) == 'b'
assert next(i) == 'c'
When no more data are available a StopIteration
exception is raised instead. At this point, the iterator object is exhausted and any further calls to its __next__()
method just raise StopIteration
again. There's no has_next()
method that tests the availability of a next item without consuming it. Unavailability of a next item is tested by checking the StopIteration
exception.
try:
while True:
arg = next(i)
print(arg)
except StopIteration:
pass
If you are OK to always consume the next item, the end of the iterator can be tested with:
...
next(i, None) is not None
Removing an Element while Iterating
It does not seem that Python iterators can remove elements while iterating, similar to Java's Iterator.remove()
. To achieve the same behavior, use a list and remove an element while iterating the list.
itertools
To explore