33

How does one check if a Python object supports iteration, a.k.a an iterable object (see definition

Ideally I would like function similar to isiterable(p_object) returning True or False (modelled after isinstance(p_object, type)).

0

3 Answers 3

88

You can check for this using isinstance and collections.Iterable

>>> from collections.abc import Iterable # for python >= 3.6
>>> l = [1, 2, 3, 4]
>>> isinstance(l, Iterable)
True

Note: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.9 it will stop working.

Sign up to request clarification or add additional context in comments.

3 Comments

This works only for new-style classes and requires Python 2.6+.
Just take note that str is also iterable. So if you're thinking tuple, list, set.. it might be better to just check for those 3 types
Use from collections.abc import Iterable as importing the ABCs from collections is depracated since Python 3.3
10

Try this code

def isiterable(p_object):
    try:
        it = iter(p_object)
    except TypeError: 
        return False
    return True

Comments

4

You don't "check". You assume.

try:
   for var in some_possibly_iterable_object:
       # the real work.
except TypeError:
   # some_possibly_iterable_object was not actually iterable
   # some other real work for non-iterable objects.

It's easier to ask forgiveness than to ask permission.

5 Comments

Wouldn't this break if something is iterable but has no entries? E.g. if you wanted to tell apart a list and a string, it wouldn't seem like a smart idea to treat it as a string just because the list is empty.
I disagree with this. Chances are that you'll want to do if not iterable: [obj] to assert that something is ALWAYS iterable. More likely to make for cleaner code imo.
Catches any TypeError in “the real work” without a way to distinguish between the two.
@WillS Absolutely, that's how I do it too. This allows you to create functions allowing parameters that can be either a unique object or a bunch of objects.
It's generally accepted that one should not use exceptions for non-exceptional (expected) situations. Thus, for something like a function that takes a parameter that may be a single value or a collection of values, isinstance(x, Iterable) would be better from that point of view. (In Python, however, one must be careful with strings and bytestrings if you're considering those to be "single" values.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.