Categories
Python Answers

How to convert all of the items in a list to floats with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *