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

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

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



Subscribe to our YouTube Channel here



plus2net.com







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 Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer