The most Pythonic way to convert a list of strings to a list of floats is to use the list comprehension
floats = [float(x) for x in strings]. It iterates over all elements in the list and converts each list element
x to a float value using the
float(x) built-in function.
This article shows you the simplest ways to convert a one-dimensional list consisting only of strings to a list of floats.
Problem: Given a list of strings
["1", "2.0", "-3.4"]. How to convert it to a list of floats
[1.0, 2.0, -3.4]?
Method 1: List Comprehension
Suppose we have a list:
a = ["1", "2.0", "-3.4"]
Now, check the type of the first list element:
print(type(a)) # <class 'str'>
print([float(x) for x in a]) # [1.0, 2.0, -3.4]
💡 List comprehension is a compact way of creating lists. The simple formula is
[expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of
You can watch me explain list comprehensions in this video:
Check the type of numbers in the new list:
A = [float(x) for x in a] print(type(A)) # <class 'float'>
The built-in function
float() converts a string to a float. Thus, it helps us create a new list of floats from the list of strings in a single line of code.
Method 2: Map Function
The built-in function
map is well optimized and efficient, when it is called, the elements of the list are retrieved upon access. Therefore, one element is stored and processed in memory, which allows the program not to store the entire list of elements in the system memory.
Apply to the same list
a the following code:
a = ["1", "2.0", "-3.4"] print(list(map(float, a))) # [1.0, 2.0, -3.4]
map() function applies the first argument, a function, to each element in an iterable. It transforms each element in the original iterable to a new element and returns a new iterable
map object of transformed values. To obtain a list, you need to convert it using the built-in
You can watch my explainer video of the map function here:
Method 3: For Loop
Of course, you can also convert a list of strings to a list of floats using a simple for loop. This is what most people coming from a programming language such as Java and C++ would do as they don’t know the most Pythonic way of using list comprehension, yet (see Method 1).
a = ["1", "2.0", "-3.4"] floats =  for element in a: floats.append(float(element)) print(floats) # [1.0, 2.0, -3.4]
This basic method to convert a list of strings to a list of floats uses three steps:
- Create an empty list with
floats = .
- Iterate over each string element using a
forloop such as
for element in list.
- Convert the string to a float using
float(element)and append it to the new float list using the
Method 4: List Comprehension + eval()
You can also use the
eval() function in a list comprehension to convert a list of strings to a list of floats:
a = ["1.0", "2.0", "-3.4"] floats = [eval(x) for x in a] print(floats) # [1.0, 2.0, -3.4]
💡 Python’s built-in
eval(s) function parses the string argument
s into a Python expression, runs it, and returns the result of the expression. If the “expression” is a simple float representation, Python converts the argument
s to a float.
But note that if you have a mixed string list with integer and float representations, your resulting list will also contain mixed data types:
a = ["1", "2.0", "-3.4"] floats = [eval(x) for x in a] print(floats) # [1, 2.0, -3.4]
You can watch me introducing the ins and outs of the
eval() function in this short guide:
To boost your Python skills the easy way, feel free to join my free email academy with lots of free content and cheat sheets—if you haven’t already! 🙂
If you want to go all-in and learn Python while getting paid in the process, check out my Python freelancer course—the number one freelance developer education in the world!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.