next()
is a method provided by Python programming language in order to iterate one step on an iterable object. next() is mostly used by iterable objects or variables like lists, tuples, file lines, etc. In this tutorial, we will examine the next() method in detail.
next() Method Syntax
The next() method is a built-in method that is directly used. It has the following syntax where two parameters can be used and one parameter is required and one parameter is optional.
next( iterable , default)
- iterable is an iterable object where next item will be returned. iterable parameter is required.
- default is an optional parameter where if there is no item in the iterable to return the default parameter value will be returned.
Make List, Tuple Iterable
In order to use a list with the next() method, the list should be converted into the iterable format or object. The iter() method can be used to convert a list, tuple, etc. into an iterable object easily. iter() method accepts the list or tuple as a parameter and returns the iterable object.
names = [ "ahmet" , "ali" , "baydan" , 1 , 2 , 3 ]
names_iterable = iter(names)
type( names_iterable )
numbers = ( 1 , 2 , 3 )
numbers_iterable = iter(numbers)
type( numbers_iterable )

Even we have made the list named names and tuple named numbers iterable the iter() method can be used to convert other data types into iterable version too. We can see that the converted data types are list_iterable
or tuple_iterable
in these examples.
Iterate with next() Method
We have learned how to make a list or tuple iterable. Now we will use the next() method in order to make iterate over created iterable types named list_iterator and tuple_iterator. Every time the next() method will iterate the next item in the iterable object.
names = [ "ahmet" , "ali" , "baydan" , 1 , 2 , 3 ]
names_iterable = iter(names)
print( next( names_iterable ) )
print( next( names_iterable ) )
print( next( names_iterable ) )
print( next( names_iterable ) )
print( next( names_iterable ) )
print( next( names_iterable ) )
numbers = ( 1 , 2 , 3 )
numbers_iterable = iter(numbers)
print( next( numbers_iterable ) )
print( next( numbers_iterable ) )
print( next( numbers_iterable ) )
print( next( numbers_iterable ) )

Set Default Value for next() Method
next() method iterates to the next item or next step every time it is called. But what will happen if there is no next item or simply the list is finished? By default, the call of the next() method will throw an exception called StopIteration
. We can prevent this exception and return a default value if reached at the end of the iterable object.
numbers = ( 1 , 2 , 3 )
numbers_iterable = iter(numbers)
print( next( numbers_iterable , -1 ) )
print( next( numbers_iterable , -1 ) )
print( next( numbers_iterable , -1 ) )
print( next( numbers_iterable , -1 ) )
print( next( numbers_iterable , -1 ) )

Iterate Over File Lines with next() Method
As an iterator method, the next() method can be used to iterate over an opened file to iterate between lines. First, the file will be opened with the open()
method. The file should be opened with a read mode. The opened files will be provided to the next() method and in every call of the next() method next line of the file will be read and returned as a string. The retuned line will be set the variable named line and this will be printed to the standard output with the print() method.
f = open( "test.txt" , "r" )
for i in range(4):
line = next(f)
print( line )
