Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

iter

iter() returns an iterator for an iterable object, or repeatedly invokes a callable until its result equals a supplied sentinel. It supports both protocol-based iteration and callable-driven iteration.

How it works

iter() produces an iterator from an iterable object, such as a list or dictionary. With a callable and a sentinel, it repeatedly calls the callable and stops when the returned value equals the sentinel.

Syntax

iter(object, sentinel, /)

Parameters

NameTypeDescription
objectobjectAn iterable object, or a callable when sentinel is supplied.
sentinelobjectA stopping value used with callable; iteration ends when a call returns it.

Returns

An iterator object.

Key points

  • The function’s result is an iterator that can be consumed incrementally.
  • Objects commonly provide iteration through an __iter__() method.
  • Objects can also qualify through indexed access beginning with integer index zero.
  • Adding a sentinel switches the first argument from an iterable object to a callable producer.

Common mistakes

  • The one-argument form requires an object that supports iteration; otherwise it raises TypeError.

Example: Create and consume a list iterator

values = [10, 20, 30]
iterator = iter(values)

print(next(iterator))
print(list(iterator))

Creates an iterator from a list, consumes its first item explicitly, then collects the remaining items.

When to use it

Iterate over collections

Process a collection lazily without indexing or copying it.

Sentinel-controlled iteration

Read successive values from a producer until a designated end value appears.

Use protocol-compatible objects

Consume a custom object that implements Python’s iterable or sequence protocol.

Interview questions

QWhat does the one-argument form of iter() accept?

iter(object) returns an iterator for an object supporting the iterable protocol through __iter__(), or the sequence protocol through integer __getitem__() calls starting at zero. The iterator then supplies values one at a time.

Follow-up: What does iter(callable, sentinel) do? It repeatedly calls the callable and stops when its result compares equal to the sentinel.

QHow does the callable-and-sentinel form terminate?

The two-argument form calls the first argument repeatedly and returns an iterator over those results. Iteration ends when a result equals the sentinel value.

Follow-up: Why is this useful for reading fixed-size chunks? A callable can fetch one chunk per iteration, while an empty chunk sentinel terminates the loop.

QWhat is the practical difference between an iterable and the iterator returned by iter()?

iter() returns an iterator object, which is consumed by operations such as next() and for. Once exhausted, subsequent retrieval attempts follow normal iterator exhaustion behavior.

Follow-up: How can a loop avoid manually handling exhaustion? A for loop repeatedly requests values and stops when the iterator is exhausted.

  • next()
  • aiter()
  • anext()
  • __iter__()
  • __getitem__()

Source: Built-in functions. Checked 2026-10-16.