search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Python | next method

schedule Aug 12, 2023
Last updated
local_offer
Python
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Python's next(~) method returns the next item in an iterator.

Parameters

1. iterator | iterator

The iterator for which we should return the next item.

2. default | any | optional

The value to return in case we have reached the end of the iterator.

Return value

The return value depends on the following cases:

Case

Return value

Have NOT exhausted all items in iterator

Next item in iterator

Have exhausted all items in iterator and default argument provided

default value

Have exhausted all items in iterator and no default argument provided

StopIteration

error

Examples

Basic usage

To obtain the next item in the iterator test:

a = ['hi', 'bye', 'see you']
# converting the list to an iterator called test
test = iter(a)
print(next(test))
print(next(test))
print(next(test))
hi
bye
see you

Note that each time we call the next(~) method we return the next item in the iterator test.

Default parameter

To specify default value of 'This is the default value':

b = ['hi', 'bye', 'see you']
# converting the list to an iterator called test
test = iter(b)
print(next(test, 'This is the default value'))
print(next(test, 'This is the default value'))
print(next(test, 'This is the default value'))
print(next(test, 'This is the default value'))
hi
bye
see you
This is the default value

On the fourth call of the next(~) method we return 'This is the default value' as we have exhausted all items in the iterator test.

StopIteration Error

If we do not provide a default value and we reach the end of the iterator:

c = ['hi', 'bye', 'see you']
# converting the list to an iterator called test
test = iter(c)
print(next(test))
print(next(test))
print(next(test))
print(next(test))
hi
bye
see you
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-11-74eba8c03080> in <module>
7 print(next(test))
8 print(next(test))
----> 9 print(next(test))
StopIteration:

We see that StopIteration exception is raised as there are no more items in the iterator test.

robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!