ValueError: could not convert string to float: '[231.49377550490459]'
<p>I am trying to cast a python list into a float. 
This is the problem narrowed down:</p>

<pre><code>loss = ['[228.55112815111235]', '[249.41649450361379]']
print(float(loss[0]))
</code></pre>

<p>And results in the error:</p>

<pre><code>ValueError: could not convert string to float: '[231.49377550490459]'
</code></pre>

<p>Can anyone help me?</p>

<p>That is because your <code>float</code> value is encapsulated within brackets. And you'll get <code>ValueError</code> because that is not valid float value. In order to convert it, you have to firstly remove them. For example:</p>

<pre><code>&gt;&gt;&gt; my_val = '[228.55112815111235]'
&gt;&gt;&gt; print float(my_val[1:-1])
228.551128151
</code></pre>

<p>In your case, you have to write:</p>

<pre><code>&gt;&gt;&gt; loss = ['[228.55112815111235]', '[249.41649450361379]']
&gt;&gt;&gt; float(loss[0][1:-1])
228.55112815111235
</code></pre>

<p>In case you want to convert entire list to list of <code>float</code>, you may use <code>map()</code> function as:</p>

<pre><code>&gt;&gt;&gt; map(lambda x: float(x[1:-1]), loss)
[228.55112815111235, 249.4164945036138] 
</code></pre>

python