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>The list you have posted is perfectly ok with your method.</p>

<ul>
<li><p>Either you have <code>a</code> already define to a string and don't have a pointed to that list. </p></li>
<li><p>The list contains a number. </p></li>
</ul>

<p>Just to verify try</p>

<pre><code>map(int,a) #if no errors you have all numbers
</code></pre>

<p>Then try </p>

<pre><code>set(map(type,a)) #if outputs {str} you should be good. 
</code></pre>

<p>Either way your error is not reproducible and post more details in your question. </p>

python list text