Iterable vs Iterator — Python

Diane Khambu
Analytics Vidhya
Published in
3 min readSep 27, 2021
Photo by Rick van der Haar on Unsplash

We all scan items for various purpose. In computing too we scan collection of items. Iterable and Iterator are concepts with scanning. I am glad to have come across this concept and would like to share it.

In this post, we’ll go over what is an iterable and iterator with examples. Let’s dive in. 🙌

Iterable is an object that implements __iter__ dunder method.

Iterator is an object that implements __iter__ and __next__ dunder method.

We see iterators are iterables but they are iterables that get exhausted. On the other hand, iterables never get exhausted because they always return a new iterator that is used to iterate.

Let’s see an example:

Here is an output:

First iteration...
daffodil
rose
gerbera
orchid
carnation
peony
lily
2nd iteration...instantiating BouquetIterator once again...
3rd iteration...
daffodil
rose
gerbera
orchid
carnation
peony
lily

You see, after you iterate for the first time, iterator is exhausted. Hence, the 2nd iteration is empty. You need to reinstantiate your iterator to be able to re-iterate again.

How can we make iterator inexhaustible? Well, it’s where iterable comes in. What iterable does is return iterator with its __iter__ dunder method. Let’s convert the above example to iterable, so that we can iterate as many times without iterator getting exhausted.

On running the file, we get:

First iteration...
daffodil
rose
gerbera
orchid
carnation
peony
lily
2nd iteration...
daffodil
rose
gerbera
orchid
carnation
peony
lily
3rd iteration...
daffodil
rose
gerbera
orchid
carnation
peony
lily

See, we can iterate as many times as we want without having to reinstantiate iterator with collection of items.

You might think why is this cool? Well, it’s cool because if you have received data from external sources that takes a lot of resource and time, you now don’t have to make that external call every time you need to iterate over them. Call once, and iterate to your heart’s content!

We can also call iterator to sequence type that has implemented just __getitem__ , as in list. We have iter() method to convert sequence type to iterator.

Let’s see an example:

See, we converted list to list_iterator using iter() method and used next() method to get items. We can also use normal for loop to iterate rest of the elements in the iterator.

See cell #4, once an iterator is exhausted, there are no more elements in it. Hence, no output.

In conclusion,

  • Iterables are inexhaustible that implement __iter__ method while iterators are exhaustible that implement __iter__ and __next__ method.
  • Iterables are able to be inexhaustible because in __iter__ method it returns iterator object that iterates over collection. While in iterator, __iter__ return itself.

That is all for this post. Thank you for reading and I hope it was a helpful article.

Inspiration:

You can support me on Patreon!

--

--