Python next() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in

Definition

In Python, next(iterator) asks an iterator for its next item. When there are no items left, it raises StopIteration, unless you pass a second argument: next(iterator, default) then returns the default instead.

It only works on iterators, not on plain lists; call iter() first. Combined with a generator expression, next(..., None) is the idiomatic way to get the first matching item or None.

next() is a built-in function: it lives in the builtins module, so it is always available without an import.

Syntax

Pass an iterator and, optionally, a default.

python
item = next(iterator)
item = next(iterator, default)
CallWhat it does
next(iter([1, 2]))Returns 1.
next(it, None)Returns None when it is exhausted.
next(x for x in xs if x > 0)The first positive number.
next([1, 2])Raises TypeError: a list is not an iterator.

Each call moves the iterator forward; you cannot go back.

Library declarations
python
next(iterator, /)
next(iterator, default, /)
# calls type(iterator).__next__(iterator)

Parameters

ParameterDescription
iteratorRequired. An object with __next__(), such as the result of iter() or a generator.
defaultOptional. Returned instead of raising StopIteration when the iterator is exhausted.

Return Value

Return ValueDescription
anyThe next item, or default if given and the iterator is empty.

Python Examples

Example 1 — Read Items Until the Iterator Is Empty

Pull two items, use a default for the third, then let next() raise.

python
it = iter([10, 20])

print(next(it))
print(next(it))
print(next(it, "done"))

try:
    next(it)
except StopIteration:
    print("StopIteration: the iterator is empty")

How It Works

  1. In order. The first two calls return 10 and 20.
  2. Default instead of an error. The iterator is empty, so next(it, "done") returns "done".
  3. No default, no mercy. The same call without a default raises StopIteration.
  4. Still empty. Returning the default does not reset the iterator.

Example 2 — Find the First Matching Item

Combine a generator expression with a default to find the first admin, or None.

python
users = [
    {"name": "ana", "admin": False},
    {"name": "ben", "admin": True},
    {"name": "cy", "admin": True},
]

first_admin = next((u["name"] for u in users if u["admin"]), None)
print(first_admin)

first_zoe = next((u["name"] for u in users if u["name"] == "zoe"), None)
print(first_zoe)

How It Works

  1. Lazy search. The generator checks users one by one and stops at ben; cy is never examined.
  2. Extra parentheses. With a second argument, the generator expression needs its own brackets.
  3. No match. Nobody is named zoe, so the default None is returned instead of an exception.
  4. Cheaper than a list. Building a full list just to take [0] would scan everything.

Example 3 — Step a Generator and Skip a Header Row

Advance a generator by hand, then consume the first line of CSV-like data separately.

python
def countdown(n):
    while n > 0:
        yield n
        n -= 1


gen = countdown(3)
print(next(gen), next(gen))
print(list(gen))

rows = iter(["name,score", "ana,90", "ben,85"])
header = next(rows)
print(header.split(","))
for row in rows:
    print(row.split(","))

How It Works

  1. Generators are iterators. next(gen) runs the function until the next yield.
  2. Paused state. After 3 and 2, only 1 is left for list(gen).
  3. Header first. One next(rows) takes the header line off the iterator.
  4. Loop over the rest. The for loop continues from the second line, so the header is not repeated.

Frequently Asked Questions

It returns the next item from an iterator. Without a default, it raises StopIteration when the iterator is exhausted.
Pass a default: next(it, None). Or iterate with a for loop, which handles the end for you.
A list is iterable but not an iterator. Use next(iter(my_list)).
next((x for x in items if condition(x)), None) returns the first match or None.
No. Iterators only move forward. Keep the items in a list if you need random access.

Next: object()

The base class of every Python class.

object() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful