Using Python, the easiest way to check if an object is iterable is to use the Python hasattr() function to check if the object has the “__iter__” attribute.

if hasattr(obj, "__iter__"):
    print("Object is iterable!")
else:
    print("Object is not iterable!")

You can also use the collections module in Python to see if the variable is an instance of the Iterable class.

from collections.abc import Iterable

if isinstance(obj, Iterable):
    print("Object is iterable!")
else:
    print("Object is not iterable!")

The last way you can check if an object is iterable is with the Python iter() function.

try:
    iter(obj)
    print("Object is iterable!")
except TypeError:
    print("Object is not iterable!")

When working with objects in Python, it is useful to be able to easily check if an object is a certain type or instance of a class.

We can check if an object is iterable in Python easily. An iterable object is any object that can be looped over. For example, lists are iterable.

The easiest way to check if an object is iterable is to use the Python hasattr() function and check if the object has the attribute “__iter__”.

Below is some sample Python code which will check if an object is iterable.

def isIterable(obj):
    if hasattr(obj, "__iter__"):
        return "Object is iterable!"
    else:
        return "Object is not iterable!"

print(isIterable([0,1,2]))
print(isIterable(0))
print(isIterable("A string"))

#Output:
Object is iterable!
Object is not iterable!
Object is iterable!

Checking if an Object is Iterable with Python isinstance() Function

We can also check if an object is iterable in Python with the isinstance() function.

We use the collections module in Python and will see if the variable is an instance of the Iterable class.

Below is an example in Python of how to use the isinstance() function to see if an object is iterable.

from collections.abc import Iterable

def isIterable(obj):
    if isinstance(obj, Iterable):
        return "Object is iterable!"
    else:
        return "Object is not iterable!"


print(isIterable([0,1,2]))
print(isIterable(0))
print(isIterable("A string"))

#Output:
Object is iterable!
Object is not iterable!
Object is iterable!

Checking if Object is Iterable with iter() Function in Python

The last way you can check if an object is iterable is with the Python iter() function.

The iter() function checks whether the object implements __iter__, and calls that to obtain an iterator.

If the function fails to find __iter__, then it will raise a TypeError.

Below is an example in Python of checking if an object is iterable with the iter() function.

def isIterable(obj):
    try:
        iter(obj)
        return "Object is iterable!"
    except TypeError:
        return "Object is not iterable!"

print(isIterable([0,1,2]))
print(isIterable(0))
print(isIterable("A string"))

#Output:
Object is iterable!
Object is not iterable!
Object is iterable!

Hopefully this article has been useful for you to learn how to check if an object is iterable or not in Python.

Categorized in:

Python,

Last Update: March 20, 2024