Python next() method

20 Jun, 2022
Python next() function returns the next item of an iterator. In this article, we will cover next() syntax, next() parameters, next() returns.
Syntax : next(iter, stopdef)
Parameters : 
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.
Here we will see the python next() in loop.
Python3
# Python code to demonstrate# working of next() # initializing listlist1 = [1, 2, 3, 4, 5] # converting list to iteratorlist1 = iter(list1) print("The contents of list are : ") # printing using next()# using defaultwhile (1):    val = next(list1, 'end')    if val == 'end':        print('list end')        break    else:        print(val)
Output:
The contents of list are : 
1
2
3
4
5
list end
Python3
list1 = [1, 2, 3, 4, 5] # converting list to iteratorlist1 = iter(list1) print(list1)print(next(list1))print(next(list1))print(next(list1))
Output:
<list_iterator object at 0x0000021D7C801D88>
1
2
3
Python3
list1 = [1, 2, 3, 4, 5] # converting list to iteratorlist1 = iter(list1) print(list1)print(next(list1, -1))print(next(list1, -1))print(next(list1, -1))print(next(list1, -1))print(next(list1, -1))print(next(list1, -1))print(next(list1, -1))
Output:
<list_iterator object at 0x0000021D7AE08908>
1
2
3
4
5
-1
-1
Python3
list1 = [1, 2, 3, 4, 5] # converting list to iteratorlist1 = iter(list1) print(list1)print(next(list1))print(next(list1))print(next(list1))print(next(list1))print(next(list1))print(next(list1))
Output:
<list_iterator object at 0x0000021D7ADF55C8>
1
2
3
4
5
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-17-02f0e1df3bbe> in <module>
    10 print(next(list1))
    11 print(next(list1))
---> 12 print(next(list1))

StopIteration: 
While calling out of the range of iterator then it rises Stopiteration error, to avoid this error we will use the default value as an argument.
Python3
# Python code to demonstrate# next() vs for loopimport time # initializing listlist1 = [1, 2, 3, 4, 5] # keeping list2list2 = list1 # converting list to iteratorlist1 = iter(list1) print("The contents of list are : ") # printing using next()# using defaultstart_next = time.time()while (1):    val = next(list1, 'end')    if val == 'end':        break    else:        print(val)print("Time taken for next() is : " + str(time.time() - start_next)) # printing using for loopstart_for = time.time()for i in list2:    print(i)print("Time taken for loop is : " + str(time.time() - start_for))
Output: 
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: Python next in For loop is a better choice when printing the contents of the list than next().
Applications: next() is the utility function for printing the components of the container of iter type. Its usage is when the size of the container is not known or we need to give a prompt when the list/iterator has exhausted.
kumar_satyam
vinayedula
Python-Built-in-functions
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.