What are Iterables and Iterators in Python

Breaking the ‘iterating confusion’ around these terms.

Iffat Malik Gore
The Startup

--

Photo by Matt Seymour on Unsplash

While writing an article about Generators, I realized that though iteration, iterable and iterator are so commonly used in programming; there is a certain degree of confusion around them. It’s crucial to understand the underlying concept for a better implementation. This is going to be a short and quick article to help us determine what is an iterable and what is an iterator.

Iteration

  • In layman’s language it is ‘repeating steps’.
  • Iteration in programming is a repetition of a block of code for a certain number of times.
  • This can be achieved by using loops.

Iterable

  • Iterable is an object which can be looped over or iterated over.
    e.g. List, Tuple, Set, Dictionary, File.
  • In simpler words, iterable is a container which has data or values and we perform iteration over it to get elements one by one. (Can traverse through these values one by one)
  • Iterable has an in-built dunder method __iter__. A simpler way to determine whether an object is iterable is to check if it supports __iter__. How? Using dir( ), it returns the list of attributes and methods supported by an object.

--

--

Iffat Malik Gore
The Startup

Technology Enthusiast | Loves Humor | Hobby Runner | Avid Reader