Skip to main content

Python next() Function

The next() function retrieves the next item from an iterator.

Syntax

next(iterator, default)

next() Parameters

Python next() function parameters:

ParameterConditionDescription
iteratorRequiredThe iterator from which the next item is to be retrieved.
defaultOptionalA default value to return if the iterator is exhausted.

next() Return Value

Python next() function returns the next item from the iterator. If the iterator is exhausted, it returns the default value passed as an argument.

danger

If the default parameter is omitted and the iterator is exhausted, it raises the StopIteration exception.

Examples

Example 1: Basic Usage of next() function

This example demonstrates basic usage of the next() function by retrieving the first item from an iterator.

my_list = iter([1,  2,  3])
print(next(my_list)) # Output: 1

output

1

Example 2: Handling StopIteration

This example shows how the next() function raises a StopIteration exception when there are no more items in the iterator.

my_list = iter([1,  2])
print(next(my_list)) # Output: 1
print(next(my_list)) # Output: 2

# This line will raise StopIteration exception
print(next(my_list))

output

1
2
Traceback (most recent call last):
File "main.py", line 6, in <module>
print(next(my_list))
StopIteration

Example 3: Using Default Value

This example demonstrates how to use the next() function with a default value to prevent the StopIteration exception when the iterator is exhausted.

my_list = iter([1,  2])
print(next(my_list)) # Output: 1
print(next(my_list)) # Output: 2

# This line will return the default value
print(next(my_list, "No more items")) # Output: "No more items"

output

1
2
No more items

Example 4: Iterating a List

This example shows iterating over a list using the next() function, demonstrating how to retrieve each item one at a time until the iterator is exhausted.

my_list = [1,  2,  3]
my_list_iter = iter(my_list)
print(next(my_list_iter)) # Output: 1
print(next(my_list_iter)) # Output: 2
print(next(my_list_iter)) # Output: 3

# This line will raise StopIteration exception
print(next(my_list_iter))

output

1
2
3
Traceback (most recent call last):
File "main.py", line 8, in <module>
print(next(my_list_iter))
StopIteration

Example 5: Iterating a String

To iterate over a string, you can convert the string into an iterator using the iter() function and then use next() to retrieve the next character.

my_string = 'Hello, World!'
my_string_iterator = iter(my_string)

print(next(my_string_iterator)) # Output: H
print(next(my_string_iterator)) # Output: e
print(next(my_string_iterator)) # Output: l

output

H
e
l

Example 6: Iterating a Tuple

To iterate over a tuple using next(), you first need to convert the tuple into an iterator using the iter() function. Then, you can call next() on this iterator to retrieve the next item.

my_tuple = ('apple', 'banana', 'cherry')
my_tuple_iterator = iter(my_tuple)

print(next(my_tuple_iterator)) # Output: apple
print(next(my_tuple_iterator)) # Output: banana
print(next(my_tuple_iterator)) # Output: cherry

output

apple
banana
cherry

Example 7: Iterating a Dictionary

For dictionaries, you can iterate over the items (key-value pairs) by calling iter() on dict.items(). Then, use next() to retrieve the next key-value pair.

content = dict(zip(range(10), range(10,  20)))
iterable = iter(content.items())

print(next(iterable)) # Output: (0, 10)
print(next(iterable)) # Output: (1, 11)
print(next(iterable)) # Output: (2, 12)

output

(0, 10)
(1, 11)
(2, 12)