32

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)).

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Boaz
  • 25,331
  • 21
  • 69
  • 77

3 Answers3

87

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.

YaOzI
  • 16,128
  • 9
  • 76
  • 72
user225312
  • 126,773
  • 69
  • 172
  • 181
  • 1
    This works only for new-style classes and requires Python 2.6+. – Sam Kauffman Apr 03 '14 at 17:55
  • 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 – Shaun Jun 01 '17 at 17:50
  • Use `from collections.abc import Iterable` as importing the ABCs from collections is depracated since Python 3.3 – Tomas P May 04 '20 at 20:33
10

Try this code

def isiterable(p_object):
    try:
        it = iter(p_object)
    except TypeError: 
        return False
    return True
Xavier Barbosa
  • 3,919
  • 1
  • 20
  • 18
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.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 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. – E. T. Dec 16 '14 at 22:57
  • 2
    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. – Will S Apr 09 '15 at 16:07
  • 8
    Catches any `TypeError` in “the real work” without a way to distinguish between the two. – Robert Siemer Sep 16 '15 at 16:27
  • @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. – Guimoute Nov 21 '19 at 14:18
  • 1
    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.) – cjs Jan 04 '20 at 09:20