How to check if an object is iterable in Python?


Iterable object is the object that can be iterated through all its elements using a loop or an iterable function. The lists, string, dictionary, tuples etc. are all known as iterable objects.

There are different ways to check if an object is iterable or not in the Python language. Let’s see them one by one.

Using a loop

In python, we have two looping techniques, one is using the ‘for’ loop and the other is using the ‘while’ loop. Using any one of these two loops, we can check whether the given object is iterable or not.

Example

In this example, we will try to iterate an object using ‘for’ loop and check whether it is iterated or not. The following is the code.

l = ["apple",22,"orange",34,"abc",0.3]
try:
   for i in l:
      print(i)
   print("Given object is iterable")
except TypeError:
   print("Given object is not iterable")

Output

apple
22
orange
34
abc
0.3
Given object is iterable

Example

Let’s see another example to check whether the given object is iterable or not using a for loop.

integer = 23454
try:
   for i in integer:
      print(i)
   print("Given object is iterable")
except TypeError:
   print("Given object is not iterable")

Output

The following is the output of the code which checks whether the given object is iterable or not.

Given object is not iterable

Using iter() Method

We have the function named iter() in python which checks whether the given object is iterable or not.

Example

In this example, we will pass the object to be iterated and the iter class to the function of the hasattr() function. Then, whether the object is iterated or not, is checked using the iter() method.

integer = 23454
if hasattr(integer, '__iter__'):
    my_iter = iter(integer)
    print("Given object is iterable")
else:
    print("Given object is not iterable")

Output

Given object is not iterable

Using collections.abc module

In python, collections.abc module provides the abstract class called Iterable, that can be used to check whether the object is iterable or not.

Example

Here, when we want to check whether the given object is iterable or not, we have to pass the object and ‘Iterable’ abstract class as arguments to the isinstance() function.

from collections.abc import Iterable
integer = 23454
if isinstance(integer, Iterable):
    print("Given object is iterable")
else:	
    print("Given object is not iterable")

Output

Following is the output produced –

Given object is not iterable

Example

Let’s see one more example to check whether the given object is iterable or not.

from collections.abc import Iterable
dic = {"name":["Java","Python","C","COBAL"],"Strength":[10,200,40,50,3]}
if isinstance(dic, Iterable):
    print("Given object is iterable")
else:
    print("Given object is not iterable")

Output

The output for the program above is displayed as –

Given object is iterable

Using try and except

We have ‘try’ and ‘except’ in python, which handles the errors if any occurred. These also checks whether the given object is iterable or not.

Example

This is an example which checks that the given object is iterable or not by using the iter() function along with the try and except.

dic = {"name":["Java","Python","C","COBAL"],"Strength":[10,200,40,50,3]}
try:
    iter(dic)
    print('Given object is iterable')
except TypeError:
    print('Given object is not iterable')

Output

Given object is iterable

Updated on: 09-Aug-2023

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements