Checking whether something is iterable

check if object is iterable javascript
python 3 check if iterable
python check if iterable but not string
javascript check if array
iterable protocol
python check if list or tuple
iterable properties

In the MDN docs:

The for...of construct is described to be able to iterate over "iterable" objects. But is there a good way of deciding whether an object is iterable?

I've tried to find common properties for arrays, iterators and generators, but have been unable to do so.

Aside for doing a for ... of in a try block and checking for type errors, is there a clean way of doing this?

The proper way to check for iterability is as follows:

function isIterable(obj) {
  // checks for null and undefined
  if (obj == null) {
    return false;
  return typeof obj[Symbol.iterator] === 'function';

Why this works (iterable protocol in depth):

Since we are talking about for..of, I assume, we are in ES6 mindset.

Also, don't be surprised that this function returns true if obj is a string, as strings iterate over their characters.

How to check if an object is iterable in Python?, You can check for this using isinstance and collections.Iterable >>> from collections import Iterable >>> l = [1, 2, 3, 4] >>> isinstance(l, Iterable)  In Python <= 2.5, you can’t and shouldn’t – iterable was an “informal” interface. But since Python 2.6 and 3.0 you can leverage the new ABC (abstract base class) infrastructure along with some builtin ABCs which are available in the collections module:

Why so verbose?

const isIterable = object =>
  object != null && typeof object[Symbol.iterator] === 'function'

How to check whether an object is iterable in Python ?, This article describes the different methods that could be used to check whether a given object in Python is an iterable or not. It also briefly explains what an  An object is iterable if it implements the iterator protocol. A good enough check to see if it does is to check for the presense of the __iter__() method. The way to do it is: hasattr(object,'__iter__') You are correct in the fact that you check if an object is iterable rather than using isinstance to check if it is of a partucular type.

The simplest solution is actually this:

function isIterable (value) {
  return Symbol.iterator in Object(value);

Object will wrap anything which isn't an object in one, allowing the in operator to work even if the original value is not an Object. null and undefined are turned into empty objects so there's no need for edge case detection, and strings get wrapped into String objects which are iterable.

In Python, how do I determine if an object is iterable , Checking for __iter__ works on sequence types, but it would fail on e.g. strings in Python 2. I would like to know the right answer too, until then, here is one  With lists, it is common to test whether the list is empty and perform special code for the empty case. With iterators, this becomes awkward -- testing whether the iterator is empty will use up the first item! The solution is an idiom based on itertools.tee().

As a sidenote, BEWARE about the definition of iterable. If you're coming from other languages you would expect that something you can iterate over with, say, a for loop is iterable. I'm afraid that's not the case here where iterable means something that implements the iteration protocol.

To make things clearer all examples above return false on this object {a: 1, b: 2} because that object does not implement the iteration protocol. So you won't be able to iterate over it with a for...of BUT you still can with a

So if you want to avoid painful mistakes make your code more specific by renaming your method as shown below:

 * @param variable
 * @returns {boolean}
const hasIterationProtocol = variable =>
    variable !== null && Symbol.iterator in Object(variable);

How to check whether an object is iterable in Python, We could make an object of a user defined class iterable either by implementing the __iter__ method or the __getitem__ method. >>> from  Check if element exist in list based on custom logic Python any() function checks if any Element of given Iterable is True. Let’s use it to check if any string element in list is of length 5 i.e.

Nowadays, as already stated, to test if obj is iterable just do

obj != null && typeof obj[Symbol.iterator] === 'function' 
Historical answer (no more valid)

The for..of construct is part of the ECMASCript 6th edition Language Specification Draft. So it could change before the final version.

In this draft, iterable objects must have the function iterator as a property.

You can the check if an object is iterable like this:

function isIterable(obj){
   if(obj === undefined || obj === null){
      return false;
   return obj.iterator !== undefined;

is_iterable - Manual, is_iterable — Verify that the contents of a variable is an iterable value variable is accepted by the iterable pseudo-type, i.e. that it is either an array or an object  Iterable was built from the ground up for speed and scale. Email, mobile push, SMS, in-app notifications, web push, social and more were engineered to work together seamlessly. Personalize each channel to serve a unique purpose as you build a cohesive lifecycle experience for your customers.

Iteration protocols, In order to be iterable, an object must implement the @@iterator Note: It is not possible to know reflectively whether a particular object  An iterable is something you can loop over. An iterator is an object representing a stream of data. It does the iterating over an iterable. Additionally, in Python, the iterators are also iterables which act as their own iterators. However, the difference is that iterators don’t have some of the features that some iterables have.

Iterables, But there are many other built-in objects, that are iterable as well. If an object isn't technically an array, but represents a collection (list, set) of for ( let char of "​test" ) { // triggers 4 times: once for each character alert ( char )  This primality check is asking “do any numbers evenly divide the candidate number”. Note that this function returns as soon as it finds a divisor, so it only iterates all the way through the number range when the candidate number is prime. Let’s take a look at how we can rewrite this function using all. What’s all ?

How do I check if an object is a collection in Python 3? : learnpython, abc.Iterable but you'll possibly end up with a generator, iterator, set, mapping or another iterable type when you were expecting a Sequence. Check if the item/attr (whatever your syntax gets in Python) exists, if yes use that Check if the attr/item (the "opposite" of what your syntax would do in Python) exists, if yes use that Fail if neither exist So x.items checks for the attribute and since one exists it uses that.