Python
iterator
count
elements
programming

Getting number of elements in an iterator in Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

An iterator in Python does not generally know its own length. Unlike a list or tuple, it produces values one at a time and may even represent an infinite stream. That is why len(iterator) usually fails.

If you need the number of elements, the core question is whether you are willing to consume the iterator. In most cases, counting an iterator means iterating through it, which leaves it exhausted afterward.

The Direct Counting Pattern

The standard way to count elements in an iterator is:

python
def count_iterator(it):
    return sum(1 for _ in it)

Example:

python
it = iter([10, 20, 30, 40])
print(count_iterator(it))

This is simple and memory-efficient because it does not store the elements. It only counts them.

The tradeoff is important: after this function runs, it is exhausted.

Why The Iterator Gets Consumed

An iterator is a one-way stream of values. Every call to next advances it. Counting requires reading every remaining item, so there is nothing left afterward.

python
it = iter([1, 2, 3])
print(sum(1 for _ in it))
print(list(it))

The second print shows an empty list because the iterator has already been consumed by the count.

When len Still Works

Sometimes people say "iterator" when they really mean an iterable container such as a list, tuple, or range. For those objects, len is the correct tool:

python
values = [1, 2, 3, 4]
print(len(values))

So the first step is to distinguish between:

  • an iterable container that can produce a fresh iterator
  • an iterator object that is already being consumed

That distinction matters much more than the counting syntax itself.

Converting To A List

Another common pattern is:

python
it = iter([10, 20, 30])
items = list(it)
print(len(items))

This works, but it stores every element in memory. For a small iterator, that is fine. For a large iterator or a stream, it can be wasteful or impossible.

Use this approach only when you also need to keep the elements for later reuse.

If You Need Both Count And Values

If you need to count the elements and still keep them, materializing them once is reasonable:

python
1it = (x * x for x in range(5))
2items = list(it)
3count = len(items)
4print(count, items)

If you need only the count, prefer the generator-based sum approach because it avoids unnecessary storage.

Sometimes The Right Answer Is len

If the source object is actually a container such as a list, tuple, or range, do not convert it into an iterator just to count it. Call len on the container directly. Counting by consumption is only the right tool when you already have a true iterator or stream and cannot know the size in advance.

Common Pitfalls

  • Calling len on a true iterator and expecting it to work.
  • Forgetting that counting consumes the iterator.
  • Converting a huge iterator to a list just to get the count.
  • Confusing iterables such as lists with iterators created from them.
  • Trying to count an infinite iterator, which never finishes.

Summary

  • True iterators usually do not know their length ahead of time.
  • The common counting pattern is sum(1 for _ in iterator).
  • Counting consumes the iterator because every element must be read.
  • 'len works on containers such as lists, not on most iterator objects.'
  • Convert to a list only when you need both the count and the stored values.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.