There are many ways to iterate over in Python.
next() method also performs the similar task over the iterator. It can be the alternative to iteration in case length is not specified and inbuilt function is not allowed to use.
Syntax : next(iter, stopdef)
iter : The iterator over which iteration is to be performed.
stopdef : Default value to be printed if we reach end of iterator.
Returns : Returns next element from the list, if not present prints the default value. If default value is not present, raises the StopIteration error.
Code #1 : Demonstrating the working of next()
The contents of list are : 1 2 3 4 5 list end
Code #2 : Performance Analysis
The contents of list are : 1 2 3 4 5 Time taken for next() is : 5.96046447754e-06 1 2 3 4 5 Time taken for loop is : 1.90734863281e-06
Result : For loop is better choice when printing the contents of list than next().
Applications : next() is the utility function for printing the components of container of iter type. Its usage is when size of container is not known or we need to give a prompt when the list/iterator has exhausted.
- Interesting Python Implementation for Next Greater Elements
- SymPy | Prufer.next() in Python
- Python - Get next key in Dictionary
- Python VLC MediaListPlayer - Playing Next Item
- Python - Split List on next larger value
- PyQt5 QSpinBox - Getting next widget in focus chain
- PyQt5 QCalendarWidget - Showing Next Month
- PyQt5 QCalendarWidget - Showing Next Year
- PyQt5 QCalendarWidget - Making focus to next-previous child
- PyQt5 QCalendarWidget - Making focus to next child
- PyQt5 QCalendarWidget - Getting next widget in focus chain
- PyQt5 QCalendarWidget - Setting Border to the Next Month button for all states
- PyQt5 QCalendarWidget - Background Color of the Next Month button
- PyQt5 QCommandLinkButton - Going to next state
- PYGLET – Step to Next Frame in Player
- PYGLET – Play Next Media in Player
- class method vs static method in Python
- Difference between Method Overloading and Method Overriding in Python
- Get() method for dictionaries in Python
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.