Sitemap
The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Member-only story

What are Iterables and Iterators in Python

Breaking the ‘iterating confusion’ around these terms.

6 min readAug 16, 2020

--

Press enter or click to view image in full size
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.

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Iffat Malik
Iffat Malik

Written by Iffat Malik

Technology Enthusiast | Loves Humor | Hobby Runner | Avid Reader