My Coding >
Programming language >
Python >
Python Iterator
Python IteratorPython IteratorPython can iterate trough many different data objects, which are iterable:
Iterable objects in Python have object iterator, which points to next elements or raise an error StopIteration if objects are finished. To call iterator directly, you need to use function iter() So you can rewrite this cycle with iterator, for example for dic:
It is more complicated, but it is explaining how this works in Python. Function next() call predefined function__next__() if it is exists. Now we can define or redefine our own __next__() function!
yield for short writingWe can use special generator yield() to make shorter writing
More tools for itertions in PythonMore tools for iterations in Pyton you can find in modeile itertools use: import itertools
|
Last 10 artitles
9 popular artitles
|
|
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |