The iter of function and next of function in python are explained in detail

  • 2020-12-19 21:07:24
  • OfStack

list, tuple, and so on are iteratable objects, and we can get iterators for these iteratable objects through the iter() function. We can then keep making ⽤ to the obtained iterator; next() function to get the ⼀ The data. The iter() function is really just the call ⽤ ___ with iterable objects Method.


>>> li = [11, 22, 33, 44, 55]
>>> li_iter = iter(li)
>>> next(li_iter) 11
>>> next(li_iter) 22
>>> next(li_iter) 33
>>> next(li_iter) 44
>>> next(li_iter) 55
>>> next(li_iter)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> 

Note: When we've iterated through the end ⼀ After the data, adjust #12132 again; The next() function throws an exception to StopIteration to tell us that all the data has been iterated over. To hold the & # 12175; next() function.


Related articles: