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>

<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>Works perfectly.</p>

<pre><code>['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>Not so much.</p>

<p>Your error message shows that one of you entries is a single quotation mark or an empty element in your list. For example, </p>

<pre><code>a = ['the', '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>throws the error:
<code>ValueError: could not convert string to float: the</code></p>

python list text