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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.