How to fix "TypeError: len() of unsized object"
<p>I am getting:</p>

<p><strong>TypeError: len() of unsized object</strong></p>

<p>after running the following script:</p>

<pre><code>from numpy import *

v=array(input('Introduce un vector v: '))
u=array(input('Introduce un vector u: '))

nv= len(v)
nu= len(u)

diferenza= 0; i=0

if nv==nu:

    while i&lt;nv:
        diferenza=diferenza + ((v[i+1]-u[i+1]))**2

    modulo= sqrt(diferenza)
    print('Distancia', v)
else:
    print('Vectores de diferente dimensiÃ³n')
</code></pre>

<p>How can I fix this?</p>

<p>The problem is that a <code>numpy</code>-<strong>scalar</strong> has no length. When you use <code>input</code> it returns a string (assuming Python3 here) and that's just converted to a numpy-string when you pass it to <code>numpy.array</code>:</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; np.array('abcdefg')
array('abcdefg', 
      dtype='&lt;U7')

&gt;&gt;&gt; len(np.array('abcdefg'))
TypeError
</code></pre>

<p>You need to parse it before you pass it to <code>numpy.array</code>. For example with <code>ast.literal_eval</code> (Thanks @PaulRooney for mentioning it):</p>

<pre><code>&gt;&gt;&gt; import ast
&gt;&gt;&gt; np.array(ast.literal_eval(input('Enter vector'))) # input "[1, 2, 3]"
array([1, 2, 3])
&gt;&gt;&gt; len(_)
3
</code></pre>

<p>If you're dealing with multidimensional <code>array</code> you have to use <code>array.size</code> instead of <code>len</code>.</p>

python arrays numpy