How to check the shape of multiple arrays contained in a list?
<p>I got a list containing multiple arrays, and I wrote the following codes try to see shape[0] of these arrays,</p>

<pre><code>for i in xrange(len(list)):
    k = list[i].shape[0]
    print k
</code></pre>

<p>the outputs were correct, but I want to <strong>check</strong> these shape[0], that is, if they are the same, the function would continue, otherwise, if they are not the same number, the function breaks. How to do this? Feel free to give me advice, thanks a lot.</p>

<h1>Update</h1>

<p>I created a list named 'ab' containing 3 different arrays, and used errors and exceptions codes to check the shape[0]:</p>

<pre><code>ab = [np.array([[1,2,3],[1,2,3]]), 
      np.array([[1,2,3]]), 
      np.array([[1,2,3],[1,2,3],[0,1,2],[0,9,9]])]

for i in xrange(len(ab)):
k = ab[i].shape[0]
print k

try:
    all(x.shape[0]==ab[0].shape[0] for x in ab)
    print 'True'
except ValueError:
    print 'False'
</code></pre>

<p>but the outputs were:</p>

<pre><code>2
1
4
True
</code></pre>

<p>the outputs were wrong, where did I make a mistake?</p>

<p>You can use a <code>set</code> comprehension to create a set of unique shapes then check if the length of the set is more than 1:</p>

<pre><code>shapes = {arr.shape[0] for arr in my_list}
if len(shapes) &gt; 1:
    # return None
</code></pre>

<p>Or as a better way try to apply a numpy function on your array, if they are not in same shape it will raise a <code>ValueError</code>:</p>

<pre><code>try:
    np.hstack(my_list)
except ValueError:
    # rasise exception or return None
</code></pre>

python arrays numpy