avatarYang Zhou

Summary

This article explains the concepts of Iterable and Iterator in Python, their differences, and how to create an Iterator.

Abstract

Iterable and Iterator are essential concepts in Python, but they are often confused with each other. This article clarifies the differences between the two and explains how to create an Iterator using the iter() function, implementing __iter__() and __next__() methods, and using Generators. The article also explains how for loops work in Python by converting an Iterable to an Iterator and using the next() function to get every element until it stops.

Bullet points

  • Iterable and Iterator are different concepts in Python.
  • Iterator is a subclass of Iterable.
  • An Iterable object can produce an Iterator using the iter() function.
  • Any object that can be iterated by a for loop is an Iterable object.
  • An Iterator is also an Iterable since it is a subclass of Iterable.
  • The difference between Iterable and Iterator is that an Iterable object can print all its elements at once, while an Iterator works as a "factory" that saves a method to produce elements one by one using the next() function.
  • An Iterator object must implement two special methods: __iter__() and __next__().
  • The __iter__() method is called on initialization of an Iterator and should return an object that has the __next__() method.
  • The __next__() method defines how the next() function works to get the next item.
  • Generator is a subclass of Iterator and can be used to create an Iterator.
  • For loops in Python apply the concepts of Iterable and Iterator to generate all items of an Iterable.

Iterable and Iterator in Python

Photo by Claire Satera on Unsplash

Introduction

Iterable and Iterator are important concepts of Python. However, sometimes it could be confusing. This post will dive into these concepts and help you totally understand them.

Iterable VS Iterator

First of all, we should know that Iterable and Iterator are different. To be exact, Iterator is a subclass of Iterable. An Iterable object, such as list ,tuple , dict , set and str , can produce an Iterator by iter() function. For example:

Intuitively, any objects that can be iterated by the for...in... loop is an Iterable object. An Iterator is also an Iterable since it’s the subclass of Iterable. We can check it using isinstance() function:

Let’s have a look at the Python 3.6 typing.py source code:

It’s very clear that Iterator is inherited from Iterable.

The difference is:

  • Iterable can print its all elements at once. It really contains all the elements.
  • Iterator works as a “factory” which saves a method to produce elements and we can only produce elements one by one using next() function. We can not print all elements at one time, cause an Iterator only knows the method of how to produce elements and never really contains any elements.

Why Python has this special design?

Because operating a large container (list/set/dict and so on) is often very time-consuming and inefficient in Python. Sometimes we don’t need to use all the elements at once, we only need to use one element at a time. Therefore, it’s a great idea that saving a method and produce an element when we need one rather than saving all elements. It reduces both time and space cost.

In a word, an Iterable object in Python is an object which can be iterated (by for loop). Iterator is subclass of Iterable but it contains an item producing method rather than items.

How to Get An Iterator?

1. Using iter() function

As shown by the previous example, it’s convenient to convert an Iterable to an Iterator by iter().

2. Implement __iter__() and __next__() methods

An Iterator object must implement two special methods: __iter__() and __next__() . We can define this two methods by ourselves and make a object be an Iterator.

The __iter__()is the method called on initialization of an Iterator. This should return an object that has __next__() method. In other words, it defines how iter() function works to return an Iterator. As the previous example shown, because Python’s list object has implemented __iter__() method, so we can using iter(my_list) to convert it to an Iterator. In a simple case, we can just return self in the __iter__() method.

The __next__() defines a method to produce items. In other words, it defines how next() function works to get the next item.

For example, this is a class to produce Fibonacci numbers:

3. Using Generator

It’s a little complex to define the above two special methods every time. Actually, Python’s generator mechanism can help us do these all.

Generator is an subclass of Iterator. Their relationships are as following:

More details of generators can be found in this post:

How for loops actually work in Python?

So far, we know all the key points of Iterable and Iterator, but there is one more point worth explaining. We said any objects that can be iterated by for loops is an Iterable. But how for loops actually work in Python? It’s simple. Just two steps:

  1. Convert an Iterator to be an Iterator.
  2. Use next() function to get every element until stop.

There is a simplest implementation of for loop to help you understand.

Conclusion

Iterable in Python is any objects which can be iterated. Iterator, which only saves a method rather than items, is a subclass of Iterable to reduce time and space cost. For loops in Python apply these concepts to generate all items of a Iterable.

Thanks for reading. If you like it, please follow my publication TechToFreedom, where you can enjoy other Python tutorials and topics about programming, technology and investment.

Python
Programming
Python3
Python Programming
Python Development
Recommended from ReadMedium