Python Iterators: Difference between revisions
(Created page with "=External= * https://docs.python.org/3/glossary.html#term-iterator =Internal= * Python Language =TODO= <font color=darkkhaki>TO PROCESS PyOOP...") |
|||
(17 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
=Internal= | =Internal= | ||
* [[Python_Language#Iterator|Python Language]] | * [[Python_Language#Iterator|Python Language]] | ||
* [[Python_Generators#Overview|Generators]] | |||
=TODO= | =TODO= | ||
<font color=darkkhaki>TO PROCESS [[PyOOP]] "The Iterator Pattern" + "Iterators" + "The iterator protocol"</font> | <font color=darkkhaki>TO PROCESS [[PyOOP]] "The Iterator Pattern" + "Iterators" + "The iterator protocol"</font> | ||
=Overview= | =Overview= | ||
An iterator instance represents a stream of data. | An iterator instance represents a stream of data. | ||
The iterator instances are created from [[Python_Language#Iterable_Types|iterable objects]] with the built-in function <code>[[Python Language Functions#iter|iter()]]</code>: | |||
<syntaxhighlight lang='py'> | |||
l = ['a', 'b', 'c'] | |||
i = iter(l) | |||
</syntaxhighlight> | |||
Once created, repeated invocations of the iterator's <code>__next__()</code> method, or by passing it to the built-in function <code>[[Python Language Functions#next|next()]]</code>, return successive items in the stream: | |||
<syntaxhighlight lang='py'> | |||
assert next(i) == 'a' | |||
assert next(i) == 'b' | |||
assert next(i) == 'c' | |||
</syntaxhighlight> | |||
When no more data are available a <code>StopIteration</code> exception is raised instead. At this point, the iterator object is exhausted and any further calls to its <code>__next__()</code> method just raise <code>StopIteration</code> again. There's no <code>has_next()</code> method that tests the availability of a next item without consuming it. Unavailability of a next item is tested by checking the <code>StopIteration</code> exception. | |||
<syntaxhighlight lang='py'> | |||
try: | |||
while True: | |||
arg = next(i) | |||
print(arg) | |||
except StopIteration: | |||
pass | |||
</syntaxhighlight> | |||
<font color=darkkhaki>If you are OK to always consume the next item, the end of the iterator can be tested with: | |||
<syntaxhighlight lang='py'> | |||
... | |||
next(i, None) is not None | |||
</syntaxhighlight> | |||
</font> | |||
=Iterators and Dictionaries= | |||
Applying <code>iter()</code> to a dictionary returns a key iterator. | |||
<syntaxhighlight lang='py'> | |||
d = {'k1': 'v1', 'k2': 'v2'} | |||
i = iter(d) | |||
keys = set() | |||
keys.add(next(i)) | |||
keys.add(next(i)) | |||
assert len(keys) == 2 | |||
assert 'k1' in keys | |||
assert 'k2' in keys | |||
</syntaxhighlight> | |||
=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, use a list and [[Python_Language_List#Remove_an_Element_while_Iterating|remove an element while iterating the list]]. | |||
=<tt>itertools</tt>= | |||
The Standard Library <code>itertools</code> module has a collection of [[Python_Generators#Overview|generators]] for many common data algorithms. | |||
<font color=darkkhaki>TODO: | |||
* To explore. | |||
* Process: https://learning.oreilly.com/library/view/python-for-data/9781098104023/ch03.html#:-:text=itertools%20module | |||
</font> | |||
=The Iterator Protocol= |
Latest revision as of 19:13, 17 May 2024
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
Iterators and Dictionaries
Applying iter()
to a dictionary returns a key iterator.
d = {'k1': 'v1', 'k2': 'v2'}
i = iter(d)
keys = set()
keys.add(next(i))
keys.add(next(i))
assert len(keys) == 2
assert 'k1' in keys
assert 'k2' in keys
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
The Standard Library itertools
module has a collection of generators for many common data algorithms.
TODO:
- To explore.
- Process: https://learning.oreilly.com/library/view/python-for-data/9781098104023/ch03.html#:-:text=itertools%20module