Convert a text list to numbers?
<p>I am trying to use <code>float()</code> to convert the following list to numbers. But it always says <code>ValueError: could not convert string to float</code>.</p>

<p>I understand that this error occurs when there is something in text that cannot be treated as a number. But my list seems okay.</p>

<pre><code>a = ['4', '4', '1', '1', '1', '1', '2', '4', '8', '16', '3', '9', '27', '81', '4', '16', '64', '256', '4', '3']

b = [float(x) for x in a]
</code></pre>

<p>you can convert list using </p>

<p>if you have any string in your list then you get error message 
<code>ValueError: could not convert string to float</code> like that </p>

<pre><code>&gt;&gt;&gt; a = ['4', '4', '1', '1', '1', '1', '2', '4', '8', '16', '3', '9', '27', '81', '4', '16', '64', '256', '4', '3']
&gt;&gt;&gt; b = list(map(float, a))
&gt;&gt;&gt; b
</code></pre>

<p>output</p>

<pre><code>[4.0, 4.0, 1.0, 1.0, 1.0, 1.0, 2.0, 4.0, 8.0, 16.0, 3.0, 9.0, 27.0, 81.0, 4.0, 16.0, 64.0, 256.0, 4.0, 3.0]
</code></pre>

python list text