We get next element from the iterator object .
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


We will display all elements of the iterator object.
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 raised.

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.
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

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer