next() Function in Python:
The next() function returns the iterator’s next item.
You can specify a default return value to be returned if the iterable has reached its limit or end.
Syntax:
next(iterable, default)
Parameters
iterable: This is required. It is an iterable object.
default: This is Optional. If the iterable has reached its limit or end, this is the default value to return.
Return Value:Â
- The next() function returns the iterator’s next item.
- If the iterator runs out of values, it returns the value passed as an argument.
- If the default parameter is omitted and the iterator becomes exhausted, the StopIteration exception is thrown.
Examples:
Example1:
Input:
Given List = ['good', 'morning', 'btechgeeks']
Output:
good morning btechgeeks Traceback (most recent call last): File "jdoodle.py", line 13, in <module> print(next(iteratr)) StopIteration
Explanation
Here it prints the iterator value of the list element and increase the iterator to next element. It raises an errors when it exceeds the length of the list.
Example2:
Input:
Given List = ['good', 'morning', 'btechgeeks'] default value = hello
Output:
good morning btechgeeks hello
Explanation
Here it prints the iterator value of the list element and increase the iterator to next element. When it exceeds the length of the list it prints the default value.
next() Function with Examples in Python
Method #1: Using Built-in Functions (Static Input)
1)Without default value
Approach:
- Give the list as static input and store it in a variable.
- Convert the given list to an iterator using the iter() function and store it in another variable.
- Apply the next() function to the above iterator to get the next item in the given list.
- Store it in another variable.
- Similarly, do the same till the end of the list.
- The Exit of the Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = ['good', 'morning', 'btechgeeks'] # Convert the given list to an iterator using the iter() function and # store it in another variable. iteratr = iter(gvn_lst) # Apply the next() function to the above iterator to get the next item in the # given list. # Store it in another variable. print(next(iteratr)) # Similarly, do the same till the end of the list. print(next(iteratr)) print(next(iteratr)) print(next(iteratr))
Output:
good morning btechgeeks Traceback (most recent call last): File "jdoodle.py", line 13, in <module> print(next(iteratr)) StopIteration
Explanation
Here it prints the iterator value of the list element and increase iterator to next element. It raises an errors when it exceeds the length of the list.
2)With the default value
Approach:
- Give the list as static input and store it in a variable.
- Give the default value as static input and store it in another variable.
- Convert the given list to an iterator using the iter() function and store it in another variable.
- Pass the above iterator and default value as the arguments to the given list to get the next item in the given list.
- Store it in another variable.
- Similarly, do the same till the end of the list.
- The Exit of the Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = ['good', 'morning', 'btechgeeks'] # Give the default value as static input and store it in another variable. gvn_defalt_valu = 'hello' # Convert the given list to an iterator using the iter() function and # store it in another variable. iteratr = iter(gvn_lst) # Pass the above iterator and default value as the arguments to the given list to # get the next item in the given list. # Store it in another variable. print(next(iteratr, gvn_defalt_valu)) # Similarly, do the same till the end of the list. print(next(iteratr, gvn_defalt_valu)) print(next(iteratr, gvn_defalt_valu)) print(next(iteratr, gvn_defalt_valu))
Output:
good morning btechgeeks hello
Explanation
Here it prints the iterator value of the list element and increase iterator to next element. When it exceeds the length of the list it prints the default value.
Method #2: Using Built-in Functions (User Input)
1)Without default value
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Convert the given list to an iterator using the iter() function and store it in another variable.
- Apply the next() function to the above iterator to get the next item in the given list.
- Store it in another variable.
- Similarly, do the same till the end of the list.
- The Exit of the Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Convert the given list to an iterator using the iter() function and # store it in another variable. iteratr = iter(gvn_lst) # Apply the next() function to the above iterator to get the next item in the # given list. # Store it in another variable. print(next(iteratr)) # Similarly, do the same till the end of the list. print(next(iteratr)) print(next(iteratr)) print(next(iteratr))
Output:
Enter some random List Elements separated by spaces = 10 20 30 10 20 30 Traceback (most recent call last): File "jdoodle.py", line 15, in <module> print(next(iteratr)) StopIteration
2)With the default value
Approach:
- Give the list as user input using list(),map(),input(),and split() functions.
- Store it in a variable.
- Give the default value as a user input using the int(input()) function and store it in another variable.
- Convert the given list to an iterator using the iter() function and store it in another variable.
- Pass the above iterator and default value as the arguments to the given list to get the next item in the given list.
- Store it in another variable.
- Similarly, do the same till the end of the list.
- The Exit of the Program.
Below is the implementation:
# Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gvn_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Give the default value as a user input using the int(input()) function and # store it in another variable. gvn_defalt_valu = int(input("Enter some random number = ")) # Convert the given list to an iterator using the iter() function and # store it in another variable. iteratr = iter(gvn_lst) # Pass the above iterator and default value as the arguments to the given list to # get the next item in the given list. # Store it in another variable. print(next(iteratr, gvn_defalt_valu)) # Similarly, do the same till the end of the list. print(next(iteratr, gvn_defalt_valu)) print(next(iteratr, gvn_defalt_valu)) print(next(iteratr, gvn_defalt_valu))
Output:
Enter some random List Elements separated by spaces = 10 20 30 Enter some random number = 40 10 20 30 40