Python built-in functions (44) -- next, python built-in 44 next

Source: Internet
Author: User

Python built-in functions (44) -- next, python built-in 44 next

English document:

next( Iterator[, Default])
Retrieve the next item from IteratorBy calling its __next__()Method. If DefaultIs given, it is returned if the iterator is exhausted, otherwise StopIterationIs raised.
Note:
  
1. The function must receive an iteratable object parameter. Each call returns the next element of the iteratable object. If all elements have been returned StopIterationException.
>>> a = iter('abcd')>>> next(a)'a'>>> next(a)'b'>>> next(a)'c'>>> next(a)'d'>>> next(a)Traceback (most recent call last):  File "<pyshell#18>", line 1, in <module>    next(a)StopIteration

2. the function can receive an optional default parameter. After the default parameter is input, if there are still elements in the iteratable object that are not returned, its element values are returned in turn. If all elements have been returned, returns the default value specified by default instead of throw.StopIterationException.

>>> a = iter('abcd')>>> next(a,'e')'a'>>> next(a,'e')'b'>>> next(a,'e')'c'>>> next(a,'e')'d'>>> next(a,'e')'e'>>> next(a,'e')'e'

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.