How to Convert a String List to a Float List in Python

4.5/5 - (6 votes)

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:

# <class 'str'>

Let’s apply the built-in function float(), and get a list of floats using list comprehension:

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 for and if statements.

You can watch me explain list comprehensions in this video:

A Simple Introduction to List Comprehension in Python

Check the type of numbers in the new list:

A = [float(x) for x in 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]

πŸ’‘ The 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 list() constructor.

You can watch my explainer video of the map function here:

Mastering the Python Map Function [+Video]

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:

# [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 for loop 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 list.append() method.

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]
# [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]
# [1, 2.0, -3.4]

You can watch me introducing the ins and outs of the eval() function in this short guide:

Python eval() -- How to Dynamically Evaluate a Code Expression in Python

Programmer Humor

Q: How do you tell an introverted computer scientist from an extroverted computer scientist?

A: An extroverted computer scientist looks at your shoes when he talks to you.

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!