What Is an Iterator in Python?

An iterator in Python is an object that gives you values one at a time.

It keeps track of where it is, so each time you ask for the next value, it moves forward. Iterators are used behind the scenes in for loops, and they are closely related to iterable objects.

A beginner-friendly way to think about it is this:

  • An iterable is something you can loop over
  • An iterator is the thing that actually returns each item one by one

Quick example

numbers = [10, 20, 30]
it = iter(numbers)

print(next(it))
print(next(it))
print(next(it))

Output:

10
20
30

Use iter() to create an iterator from an iterable, then use next() to get one item at a time.

What an iterator means

An iterator:

  • Gives values one at a time
  • Remembers its current position
  • Moves forward each time you call next()
  • Raises StopIteration when there are no items left

This is why iterators are useful when Python needs to process data step by step.

Iterator vs iterable

These two terms are related, but they are not the same.

  • An iterable is an object you can loop over
  • An iterator is the object that does the step-by-step looping

Common iterable objects include:

  • Lists
  • Tuples
  • Strings
  • Dictionaries

You usually create an iterator from an iterable with iter().

Example:

numbers = [1, 2, 3]

print(type(numbers))

it = iter(numbers)
print(type(it))

A list is iterable, but it is not itself an iterator.

If you want a deeper explanation, see what is an iterable in Python.

How iter() and next() work

The two main functions are:

  • iter(obj) creates an iterator from an iterable object
  • next(iterator) gets the next value from that iterator

Example:

letters = ["a", "b", "c"]
it = iter(letters)

print(next(it))
print(next(it))
print(next(it))

Output:

a
b
c

If you call next() again after the iterator is finished, Python raises StopIteration.

letters = ["a", "b", "c"]
it = iter(letters)

print(next(it))
print(next(it))
print(next(it))
print(next(it))

This causes:

StopIteration

That exception simply means there are no more values left. If you want to understand this error better, see StopIteration exception in Python explained.

How for loops use iterators

A for loop uses iterators automatically.

When you write this:

numbers = [10, 20, 30]

for number in numbers:
    print(number)

Python does something like this behind the scenes:

  1. Calls iter(numbers)
  2. Keeps calling next(...)
  3. Stops when StopIteration happens

So a for loop is really using the iterator protocol for you.

That is why you usually do not need to manage iterators directly. Still, understanding them helps explain how loops work and why some objects can only be used once.

Simple example to include

Here is a full example that shows how an iterator is created and consumed:

numbers = [100, 200, 300]
it = iter(numbers)

print(next(it))  # first item
print(next(it))  # second item
print(next(it))  # third item

print(next(it))  # no items left

Expected behavior:

  • The first three next() calls print values
  • The last next() call raises StopIteration

A safer version uses try and except:

numbers = [100, 200, 300]
it = iter(numbers)

try:
    while True:
        print(next(it))
except StopIteration:
    print("Iterator finished")

Output:

100
200
300
Iterator finished

When beginners should care

You do not need to use iterators directly all the time, but they matter in a few common situations:

  • When learning how for loops work
  • When using next() directly
  • When working with generator objects in Python
  • When debugging StopIteration
  • When dealing with one-time-use objects

Understanding iterators also helps when using tools like enumerate(), which work with iterable data.

Important iterator behavior

There are a few behaviors beginners should know:

  • Most iterators are consumed as you use them
  • You usually cannot restart the same iterator from the beginning
  • If you need to loop again, create a new iterator
  • Not every iterable object is itself an iterator

Example:

numbers = [1, 2, 3]
it = iter(numbers)

for value in it:
    print(value)

print("Loop again:")

for value in it:
    print(value)

Output:

1
2
3
Loop again:

The second loop prints nothing because the iterator is already exhausted.

To start over, create a new iterator:

numbers = [1, 2, 3]

it1 = iter(numbers)
it2 = iter(numbers)

print(next(it1))
print(next(it2))

Output:

1
1

Common mistakes

Beginners often run into these problems:

  • Confusing iterable objects like lists with iterator objects
  • Calling next() on a list instead of on iter(list)
  • Expecting an iterator to reset automatically
  • Using an iterator once and not realizing it is already exhausted
  • Not understanding why StopIteration appears

For example, this does not work:

numbers = [10, 20, 30]
print(next(numbers))

It raises an error because a list is not an iterator.

Correct version:

numbers = [10, 20, 30]
it = iter(numbers)
print(next(it))

Useful things to check while debugging:

type(obj)
iter(obj)
next(it)
hasattr(obj, '__iter__')
hasattr(obj, '__next__')

In simple terms:

  • __iter__ usually means an object can be iterated over
  • __next__ usually means it behaves like an iterator

FAQ

Is a list an iterator in Python?

No. A list is iterable, but it is not itself an iterator. You can create an iterator from it with iter(list_name).

What is the difference between an iterator and an iterable?

An iterable can be looped over. An iterator returns items one at a time and keeps track of its current position.

Why does next() give StopIteration?

It means the iterator has no more values left.

Do for loops use iterators?

Yes. A for loop uses the iterator protocol behind the scenes.

Can I reuse an iterator?

Usually no. Once it is exhausted, create a new iterator from the original iterable if possible.

See also