Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Check If an Object is Iterable in Python

  • 01 Dec, 2025
  • Com 2
How to Check If an Object is Iterable in Python

A Python iterable is an object that is capable of returning one element at a time, and you can use it within a for loop, comprehensions, or generators.

If you validate the iterator object, you can prevent errors like TypeError: ‘int’ object is not iterable in runtime.

Here are three ways to check: 

  1. Using collections.abc.Iterable class
  2. Using iter() with try/except
  3. Using _iter_

Method 1: Using the collections.abc.Iterable class

The clean, modern, and most Pythonic way to check if an input Iterable is an Object in Python with type safety is to use the collections.abc.Iterable class. It contains abstract base classes that provide interfaces for various container types.

You can use it with the isinstance() method, which returns True if obj is an iterable and False otherwise.

Using the collections.abc.Iterable class to Check If an Object is Iterable in Python

 

from collections.abc import Iterable


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


print("List:", is_iterable([1, 2, 3]))
# Output: List: True

print("String:", is_iterable("hello"))
# Output: String: True

print("Integer:", is_iterable(42))
# Output: Integer: False

print("Dictionary:", is_iterable({'a': 1}))
# Output: Dictionary: True

print("None:", is_iterable(None))
# Output: None: False

Method 2: Using the iter()

Another common way is to use the built-in iter() function, which raises a TypeError and returns False if the object is not iterable; otherwise, it returns True.

It works well with all iterables: lists, tuples, dicts, sets, generators, strings, range, map, filter, zip, files, etc.

Using iter() function to Check If an Object is Iterable

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


print("List:", is_iterable([1, 2, 3]))
# Output: List: True

print("String:", is_iterable("hello"))
# Output: String: True

print("Integer:", is_iterable(42))
# Output: Integer: False

print("Dictionary:", is_iterable({'a': 1}))
# Output: Dictionary: True

In this code, we also defined a custom function, is_iterable(), that accepts an object as input. We pass the object to the iter() function and check its validity, and it returns boolean values based on the input.

Method 3: Using __iter__

If you are using legacy Python code and you want to detect old-style sequence objects (pre-Python 2.2 semantics), use the __iter__ approach. If an input object is iterable, it should contain an __iter__ method by default. It returns True if the method exists, otherwise False.

Using __iter__ to Check If an Object is Iterable

 

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


print("List:", is_iterable([1, 2, 3]))
# Output: List: True

print("String:", is_iterable("hello"))
# Output: String: True

print("Integer:", is_iterable(42))
# Output: Integer: False

print("Dictionary:", is_iterable({'a': 1}))
# Output: Dictionary: True

print("None:", is_iterable(None))
# Output: None: False

That’s all!

Post Views: 11
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Select a Random Element from a List in Python
How to Create a Constant in Python

2 Comments

  1. Ashraf

    March 11, 2021 at 12:23 am

    For the last few years, I follow your article, a Great article.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend