How to Check if an Object is Iterable in Python

While working on a data processing script, I needed to loop through a list of customer records. The issue was, I wasn’t sure if the object I received from an API was even iterable.

If you’ve been coding in Python for a while, you’ve probably run into the same problem. Sometimes, you expect a list or tuple, but you end up with a single value or None. That’s when checking if an object is iterable becomes really handy.

In this tutorial, I’ll share four easy methods that I personally use to check if an object is iterable in Python. I’ll also include simple code examples so you can try them out right away.

Method 1: Use Python’s iter() function

Python provides the built-in iter() function that can be used to check if an object is iterable. The parameters provided in the iter method in Python are object and sentinel.

def is_iterable(object):
 try:
  iter(object)
  return True
 except TypeError:
  return False

print("List:",is_iterable([1, 2, 3]))
print("String:",is_iterable("hello"))
print("Integer:",is_iterable(42))
print("Dictionary:",is_iterable({'a': 1})) 

In this try part, we can add Python code that might raise an exception, whereas the expected part is the Python code with the iter() function in the try part to check if the object is iterable in Python.

 try:
  iter(object)
  return True
 except TypeError:
  return False

Output:

List: True
String: True
Integer: False
Dictionary: True

You can see the output in the screenshot below.

How to check if an object is iterable in Python

This method uses Python’s iter() function in a try-except block to determine if an object is iterable, returning True or False accordingly.

Method 2: Use for loop in Python

In this example, we will try to iterate over an object using a for loop and check whether it is iterated over or not in Python.

text = "John Smith"
try:
   for i in text:
      print(i)
   print("Given object is iterable")
except TypeError:
   print("Given object is not iterable")  

Output:

J
o
h
n
 
S
m
i
t
h
Given object is iterable

You can see the output in the screenshot below.

Python program to check if an object is iterable using a for loop

This method checks if an object is iterable by attempting to loop through it with a for loop, confirming iterability if no TypeError is raised.

Method 3: Use the collections.abc module with isinstance() function

In Python, the collections.abc module provides the Iterable abstract base class, which can be utilized to perform the iterability check. The Iterable class from this module can be subclassed to create new iterable classes in Python.

def is_iterable(object):
 return isinstance(object, Iterable)

print("List:",is_iterable([1, 2, 3]))
print("String:",is_iterable("hello")) 
print("Integer:",is_iterable(42))
print("Dictionary:",is_iterable({'a': 1})) 

Here, I will check if the object passed to the function is an example of an iterable object. Then, it returns True if an object is iterable in Python. Otherwise False.

return isinstance(object, Iterable)

Output:

List: True
String: True
Integer: False
Dictionary: True

You can see the output in the screenshot below.

Check if an object is iterable in Python using the collections.abc module with isinstance() function

This method checks an object’s iterability by verifying if it is an instance of the Iterable class from collections.abc, returning True or False.

Method 4: Use Python duck typing

In Python, duck typing means that the type or class of an object is determined by its behavior rather than its specific type. This implies that if an object behaves like an iterable, it can be considered one.

def is_iterable(obj):
  return hasattr(obj, "__iter__")

print("List:",is_iterable([5, 2, 3]))
print("String:",is_iterable("Latham")) 
print("Integer:",is_iterable(42))
print("Dictionary:",is_iterable({'a': 1}))

I have used the hasattr() function to check if the object has the __iter__ attribute in Python. It returns True if the object has the specified attribute, in this case, iter, which indicates it is iterable. Otherwise, it returns False.

return hasattr(obj, "__iter__")

Output:

List: True
String: True
Integer: False
Dictionary: True

You can see the output in the screenshot below.

How to check if an object is iterable in Python using duck typing

This method uses duck typing to check if an object has the __iter__ attribute, returning True if it’s iterable and False otherwise.

I hope this tutorial provided the information to check if an object is iterable in Python. Here, I have also illustrated the different ways to check the iterability of an object in Python, such as using the iter() function, for loop, and collections.abc module with the isinstance function and duck typing.

Knowing how to check if an object is iterable can be useful to avoid a TypeError when you have a for loop or list comprehension with the object in Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.