How to determine the dimensions of a mix of lists and arrays?
<p>Consider an object that is a list of arrays: </p>

<p><code>a=[array([1,2,3]),array(2,5,10,20)]</code></p>

<p>In its own funny way, this thing has two dimensions. The list itself is one dimension, and it contains objects which are 1D. Is there an easy way to distinguish between <code>a</code> above and a list like <code>b=[1,3,6,9,11]</code> which is simply 1D, and <code>c=1</code>, which is a 0D scalar? I want a function <code>dimens()</code> such that <code>dimens(a)</code> returns <code>2</code>, <code>dimens(b)</code> returns <code>1</code>, and <code>dimens(c)</code> returns <code>0</code>.</p>

<p>I am doing it by testing the shape of the first element in the list, but I feel like there may be a cleaner approach.</p>

<p>You can use <strong>isinstance</strong> method to distinguish between the two arrays</p>

<p>Lets consider the first list </p>

<pre><code>a = [1,2,3]
</code></pre>

<p>Here the first element is an integer hence <code>isinstance(a[0],int)</code> will return true</p>

<p>For the second array <code>b = [[1,2][3,4]]</code> the first element is an array so <code>isinstance(b[0],int)</code> will return false. 
You can check the second list by using <code>isinstance(b[0],list)</code></p>

<p>I am using list in place of array but it will work with arrays also</p>

python arrays list numpy