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.
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 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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.