w3resource

Python: next() function

next() function

The next() function is used to get the next item in an iterator.

Version:

(Python 3.2.5)

Syntax:

next(iterator[, default])

Parameter:

Name Description Required /
Optional
iterator An iterable object. Required
default This value is returned if the iterator is exhausted (no items left). Optional

Return value:

Returns the next item of the iterator.

Example: Python next() function

items = [7, 15, 'Python']

# converting list to iterator
itemsIterator = iter(items)
print(itemsIterator)

# Output: 7
print(next(itemsIterator))

# Output: 15
print(next(itemsIterator))

# Output: 'Python'
print(next(itemsIterator))

Output:

<list_iterator object at 0x7f8b758a8470>
7
15
Python

Pictorial Presentation:

Python: Built-in-function - next() function

Pictorial Presentation:

Python: Built-in-function - next() function

Python Code Editor:

Previous: min()
Next: object()

Test your Python skills with w3resource's quiz



Python: Tips of the Day

How do I check if a list is empty?

For example, if passed the following:

a = []
if not a:
  print("List is empty")

Ref: https://bit.ly/2A4JXx9