convert element in a list to float

Steven D'Aprano steve at pearwood.info
Sun Sep 13 21:09:12 EDT 2015


Hi Forums_MP and welcome,


On Mon, 14 Sep 2015 05:55 am, forums_mp at hotmail.com wrote:

> For starters, I googled and saw a plethora of writings on how to convert
> an entire list from string to float.   My interest is on select elements
> in the list.  The output from the print statement: print scenarioList
>   
> is as follows
> 
> [ '3000000', '"N"', '11400000', '"E"' ]
> 
> I need to convert the first and third element to float.
>   lat = ( float ) scenarioList [ 0 ]
>   lon = ( float ) scenarioList [ 2 ]
> 
> fails (invalid syntax).  How can I achieve my objective.


By now you have hopefully discovered that the answer is to call float as a
function:

lat = float(scenarioList[0])


but I wonder what in the documentation or examples you saw suggested to you
that Python used the C type-cast syntax `(float)value`? Whatever it was
that gave you this wrong impression needs to be fixed.




-- 
Steven




More information about the Python-list mailing list