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

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'>```

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:

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]```

💡 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:

## 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 `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]
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:

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