Skip to content
Related Articles

Related Articles

How to check if an object is iterable in Python?

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 13 Jan, 2021

In simple words, any object that could be looped over is iterable. For instance, a list object is iterable and so is an str object. The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case).  In this article, we are going to see how to check if an object is iterable in Python.


Input: “Hello”

Output: object is not iterable

Explanation: Object could be like( h, e, l, l, o)

Input: 5

Output: int’ object is not iterable

Example 1:


# code
name = "Hello"
for letter in name:
  print(letter, end = " ")


H e l l o

Example 2:


# code
number = 5 
for i in number:


TypeError: 'int' object is not iterable

However, an int object is not iterable and so is a float object. The integer object number is not iterable, as we are not able to loop over it.

Checking an object’s iterability

We are going to explore the different ways of checking whether an object is iterable or not. We use the hasattr() function to test whether the string object name has __iter__ attribute for checking iterability. However, this check is not comprehensive.

Method 1: Using __iter__ method check.


# code
name = 'Roster'
if hasattr(name, '__iter__'):
  print(f'{name} is iterable')
  print(f'{name} is not iterable')


Roster is iterable

Method 2: Using the Iterable class of module.

We could verify that an object is iterable by checking whether it is an instance of the Iterable class. The instance check reports the string object as iterable correctly using the Iterable class. This works for Python 3 as well. For Python 3.3 and above, you will have to import the Iterable class from and not from collections like so:


# code
from import Iterable
name = 'Roster'
if isinstance(name, Iterable):
  print(f"{name} is iterable")
  print(f"{name} is not iterable")


Roster is iterable

Method 3: Using the iter() builtin function.

The iter built-in function works in the following way:

  • Checks whether the object implements __iter__, and calls that to obtain an iterator.
  • If that fails, Python raises TypeError, usually saying “C object is not iterable,” where C is the class of the target object.



# code
name = "Roster"
  print("{} is iterable".format(name)) 
except TypeError:
  print("{} is not iterable".format(name))


Roster is iterable

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!