Python Function Reference

iter

Create an iterator over the items of an object.

Python 3.13
iter(object[, sentinel])
  • [ ]optional part
object
An iterable, or a function without arguments if sentinel is given.
Iterable | Callable
sentinel
A stop value: the iterator calls object until it returns sentinel.
AnyOptional

Return value

An iterator; raises TypeError for an object that cannot be iterated over.

Examples

Iterator over a list

Python 3.13
it = iter([1, 2, 3])
print(next(it))
print(next(it))
1
2

Reading until a sentinel value

Python 3.13
numbers = iter([5, 3, -1, 8])


def read():
    return next(numbers)


print(list(iter(read, -1)))
[5, 3]

Details

A for loop calls iter by itself, so you need it explicitly when you take items one at a time with next. An iterator can be used only once: after a full pass it is empty. For an iterator, iter returns the iterator itself.

See also