Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I tell whether a Python value is iterable or an iterator?

Quick answer

An iterable is a value from which iter(value) can produce an iterator. An iterator delivers values one at a time with next() and signals exhaustion with StopIteration. For example, a list is iterable; iter(the_list) gives you an iterator. Each successful call to next() advances that iterator, so it can run out. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a Python value and want to know whether it’s iterable or an iterator. Aren’t those just two names for the same thing?

Geez

They describe related but different roles. An iterable is a value from which Python can get an iterator using iter(value). An iterator delivers values one at a time when you call next(); when it has no more values, it signals exhaustion with StopIteration. [S1]

A list makes the distinction clear: the list is iterable, and iter(the_list) produces an iterator. In ordinary code, a for loop handles the iteration process for you. These terms describe how a value can be used, not just what kind of data it contains.

Follow-up question

Can you show a small test with a list and then demonstrate what happens when the iterator runs out?

Geez

Here’s a diagnostic example. Each successful next() call advances the iterator:

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

print(next(it))  # 1
print(next(it))  # 2
print(next(it))  # 3

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

The first three calls deliver the list’s values in order. Once they have all been delivered, the next call signals exhaustion with StopIteration. [S1] The try/except lets the example handle that signal and print a message. It also shows why it matters which object you use: calling next(it) consumes a value from the iterator.

Follow-up question

How can I check my own value without accidentally losing an item my program still needs?

Geez

First decide what you need to check. To see whether Python can obtain an iterator from a value, try iter(value) and handle TypeError if one is raised. This does not retrieve a value with next().

To check whether an iterator has a next value, you must try to retrieve one. If successful, that value has been consumed from the iterator. Keep it and process it as part of your program’s normal work if you still need it. If the iterator has no more values, it signals exhaustion with StopIteration. [S1] The key distinction is that obtaining an iterator and retrieving a value are different operations.

Follow-up question

What’s a reusable pattern I can keep beside my code when I’m not sure which case I have?

Geez

This pattern first tries to obtain an iterator, then—if successful—tries to retrieve one value:

value = [1, 2, 3]  # replace with your value

try:
    it = iter(value)
except TypeError:
    print("Could not get an iterator from this value")
else:
    print("Could get an iterator")
    try:
        first = next(it)
    except StopIteration:
        print("The iterator has no next value")
    else:
        print("Next value:", first)

For the list shown, the first value is 1. The first test asks whether Python can obtain an iterator; the second retrieves a value and advances that iterator. If you need to continue through the values, keep using it: an iterator advances forward and does not reset just because you call next() again. [S1]

A practical checklist

  • Identify whether you are checking the original value or an iterator you already have.

  • Use iter(value) to try to obtain an iterator; handle TypeError if appropriate.

  • Call next(iterator) only when you intend to retrieve a value.

  • Handle StopIteration when an iterator has no more values.

  • Keep a retrieved value if your program still needs to process it.

A few more useful answers

Does calling `iter()` tell me whether an iterator still has values?

No. It tries to obtain an iterator; it does not retrieve the next value. To check for a value, call next() and handle possible exhaustion.

Can I call `next()` on a list directly?

First obtain an iterator with iter(the_list), then call next() on that iterator. The documented example demonstrates successive next() calls on an iterator created from a list. [S1]

Sources

References used for this guide.

  1. [S1] Functional Programming HOWTO — Python 3.15.2 documentation
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work