Home  >  Article  >  Backend Development  >  Usage example of next() in python iterator

Usage example of next() in python iterator

黄舟
黄舟Original
2017-10-08 09:33:342624browse

>>> g = (x ** 2 for x in range(10))
>>> next(g)
0
>>> next(g)
1
>>> next(g)
4
>>> next(g)
9
>>> next(g)
16
>>> next(g)
25
>>> next(g)
36
>>> next(g)
49
>>> next(g)
64
>>> next(g)
81
>>> next(g)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Every time next is called, the iterator is iterated, and a StopIteration error will be triggered in the end.

The above is the detailed content of Usage example of next() in python iterator. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn