Sometimes, we want to convert all of the items in a list to floats with Python.
In this article, we’ll look at how to convert all of the items in a list to floats with Python.
How to convert all of the items in a list to floats with Python?
To convert all of the items in a list to floats with Python, we can use list comprehension.
For instance, we write
l = [float(i) for i in lst]
to convert all the items in the lst
list to floats with float(i) for i in lst
.
And we put all the items in a new list.
float
converts values to a floating point number.
Conclusion
To convert all of the items in a list to floats with Python, we can use list comprehension.