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 FalseOutput:
List: True
String: True
Integer: False
Dictionary: TrueYou can see the output in the screenshot below.

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 iterableYou can see the output in the screenshot below.

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: TrueYou can see the output in the screenshot below.

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: TrueYou can see the output in the screenshot below.

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:
- Python Program to Print the Number of Elements Present in an Array
- Concatenation of Arrays in Python
- Python Write Array to CSV
- Python: Repeat an Array n Times

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.