Get the length of a Generator or Iterator in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. Get the length of a Generator in Python
  2. Get the length of an Iterator in Python

# Get the length of a Generator in Python

To get the length of a generator in Python:

  1. Use the list() class to convert the generator to a list.
  2. Pass the list to the len() function, e.g. len(list(gen)).
  3. Note that once the generator is converted to a list, it is exhausted.
main.py
def g(): yield 1 yield 2 yield 3 gen = g() result = len(list(gen)) print(result) # ๐Ÿ‘‰๏ธ 3

get length of generator

The code for this article is available on GitHub

Note that once the generator is converted to a list it is exhausted.

main.py
def g(): yield 1 yield 2 yield 3 gen = g() print(len(list(gen))) # ๐Ÿ‘‰๏ธ 3 print(len(list(gen))) # ๐Ÿ‘‰๏ธ 0

converting generator to list exhausts it

You can convert the generator to a list and store the result in a variable.

main.py
def g(): yield 1 yield 2 yield 3 gen = g() my_list = list(gen) print(len(my_list)) # ๐Ÿ‘‰๏ธ 3

Now you can iterate over the list as many times as necessary.

An alternative approach is to use the built-in sum() function.

main.py
def g(): yield 1 yield 2 yield 3 gen = g() result = sum(1 for _ in gen) print(result) # ๐Ÿ‘‰๏ธ 3 print(list(gen)) # ๐Ÿ‘‰๏ธ []
The code for this article is available on GitHub

The sum() function takes an iterable, sums its items from left to right and returns the total.

We effectively replace each item in the generator with 1 and sum the values.

This solution doesn't require us to convert the generator to a list and might be less memory-consuming.

However, the difference in performance is negligible unless you're dealing with generators with many millions of items.

This approach also exhausts the generator as there is no way to get the length of a generator without iterating over it.

Make sure you aren't trying to get the length of an infinite generator, as that operation would never be completed.

# Get the length of an Iterator in Python

The same approach can be used to get the length of an iterator.

main.py
my_iterator = iter([1, 2, 3, 4, 5]) result = len(list(my_iterator)) print(result) # ๐Ÿ‘‰๏ธ 5

get length of iterator

Note that once the iterator is converted to a list it is exhausted.

main.py
my_iterator = iter([1, 2, 3, 4, 5]) print(len(list(my_iterator))) # ๐Ÿ‘‰๏ธ 5 print(len(list(my_iterator))) # ๐Ÿ‘‰๏ธ 0
The code for this article is available on GitHub

You can convert the iterator to a list and store the result in a variable.

main.py
my_iterator = iter([1, 2, 3, 4, 5]) my_list = list(my_iterator) print(len(my_list)) # ๐Ÿ‘‰๏ธ 5

Now you can iterate over the list as many times as necessary.

An alternative approach is to use the built-in sum() function.

main.py
my_iterator = iter([1, 2, 3, 4, 5]) result = sum(1 for _ in my_iterator) print(result) # ๐Ÿ‘‰๏ธ 5
The code for this article is available on GitHub

The sum() function takes an iterable, sums its elements from left to right and returns the total.

We effectively replace each item in the iterator with 1 and sum the values.

This solution doesn't require us to convert the iterator to a list and might be less memory-consuming.

However, the difference in performance is negligible unless you're dealing with iterators with many millions of elements.

This approach also exhausts the iterator as there is no way to get the length of an iterator without iterating over it.

Make sure you aren't trying to get the length of an infinite iterator, as that operation would never be completed.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev