Table of Contents
In this tutorial, we will learn about the Python next() function with the help of examples.
The next()
function returns the next item from the iterator.
Example
marks = [65, 71, 68, 74, 61] # convert list to iterator iterator_marks = iter(marks) # the next element is the first element marks_1 = next(iterator_marks) print(marks_1) # find the next element which is the second element marks_2 = next(iterator_marks) print(marks_2) # Output: 65 # 71
1. next() Syntax
The syntax of next()
is:
next(iterator, default)
2. next() Parameters
- iterator –
next()
retrieves next item from the iterator - default (optional) – this value is returned if the iterator is exhausted (there is no next item)
3. next() Return Value
- The
next()
function returns the next item from the iterator. - If the iterator is exhausted, it returns the
default
value passed as an argument. - If the default parameter is omitted and the iterator is exhausted, it raises the
StopIteration
exception.
4. Example 1: Get the next item
random = [5, 9, 'cat'] # converting the list to an iterator random_iterator = iter(random) print(random_iterator) # Output: 5 print(next(random_iterator)) # Output: 9 print(next(random_iterator)) # Output: 'cat' print(next(random_iterator)) # This will raise Error # iterator is exhausted print(next(random_iterator))
Output
<list_iterator object at 0x7feb49032b00> 5 9 cat Traceback (most recent call last): File "python", line 18, in <module> StopIteration
A list is an iterable and you can get its iterator from it by using the iter()
function in Python.
Learn more about
We got an error from the last statement in the above program because we tried to get the next item when no next item was available (iterator is exhausted).
In such cases, you can give a default value as the second parameter.
5. Example 2: Passing default value to next()
random = [5, 9] # converting the list to an iterator random_iterator = iter(random) # Output: 5 print(next(random_iterator, '-1')) # Output: 9 print(next(random_iterator, '-1')) # random_iterator is exhausted # Output: '-1' print(next(random_iterator, '-1')) print(next(random_iterator, '-1')) print(next(random_iterator, '-1'))
Output
5 9 -1 -1 -1
Note: Internally, next()
calls the __next__()
method.
Related posts:
Python String casefold()
Python eval()
Python Program to Find the Square Root
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Errors and Built-in Exceptions
Python zip()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Find the Size (Resolution) of a Image
Python Set symmetric_difference()
Python hex()
Python break and continue
Python Operator Overloading
Python String title()
Python Program to Parse a String to a Float or Int
Python Statement, Indentation and Comments
Python property()
Python Program to Sort a Dictionary by Value
Python Program to Calculate the Area of a Triangle
Python String isalpha()
Python Set copy()
Python enumerate()
Python Inheritance
Python Program to Get the Class Name of an Instance
Python Program to Check the File Size
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Convert Celsius To Fahrenheit
Python String split()
Python hash()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python String isdecimal()
Python Directory and Files Management
Python Dictionary popitem()