next()

We get next element from the iterator object .
next(iterator_obj,default_value)
iterator_obj: iterataor , from this elements are returned, StopIteration is raised if no element is left
default_value: Optional, This value is returned if no element is left, otherwise StopIteration exception is raised.

iter() to get iterator

Example

We will display all elements of the iterator object.
my_list=[18,5,15]
my_iter=iter(my_list)
print(next(my_iter)) # 18
print(next(my_iter)) # 5
print(next(my_iter)) # 15
If we add one more line with next() then StopIteration is arised.

Read more on iter() about handling the StopIteration exception.

Using default value

We can use a default value to return when there is no item left to return. In such case no exception is raised.
my_str='abc'  
my_str=iter(my_str)
print(next(my_str))       # a
print(next(my_str))       # b
print(next(my_str, 'x'))  # c
print(next(my_str, 'x'))  # x
print(next(my_str, 'x'))  # x
All Built in Functions in Python iter() Generator - Tutorials


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer