Python
Data Conversion
Lists
Float
Programming

How do I convert all of the items in a list to floats?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Python, converting every item in a list to float is usually a one-line operation, but the right version depends on your data quality. If the list is clean, use a direct conversion. If the list may contain blanks, bad strings, or None, you need a slightly more defensive approach.

Use a list comprehension for the common case

The most direct and readable solution is a list comprehension:

python
1values = ["1.2", "3", "4.75", "-2.0"]
2numbers = [float(value) for value in values]
3
4print(numbers)

This creates a new list of floating-point values:

text
[1.2, 3.0, 4.75, -2.0]

A list comprehension is usually the best default because it is explicit, easy to read, and easy to extend later if the conversion needs extra cleanup.

map works too

If you prefer a functional style, map does the same transformation:

python
1values = ["1.2", "3", "4.75", "-2.0"]
2numbers = list(map(float, values))
3
4print(numbers)

This is perfectly valid Python. The tradeoff is mostly style. Many Python developers prefer the list-comprehension version because it is easier to customize with conditions or preprocessing.

For example, if you later need to strip whitespace, the comprehension version stays very readable:

python
numbers = [float(value.strip()) for value in values]

Handle invalid values safely

Real data is often messy. If one item cannot be converted, float(...) raises ValueError and stops the entire conversion:

python
1values = ["1.2", "oops", "4.75"]
2
3# Raises ValueError
4# numbers = [float(value) for value in values]

If you want to skip invalid items, wrap the conversion in a helper:

python
1def safe_float_list(values: list[str]) -> list[float]:
2    result = []
3    for value in values:
4        try:
5            result.append(float(value))
6        except ValueError:
7            print(f"Skipping invalid value: {value!r}")
8    return result
9
10
11values = ["1.2", "oops", "4.75"]
12print(safe_float_list(values))

This is a good pattern when you are importing CSV rows, user input, or API data that may contain unexpected strings.

If None values are possible, handle them too:

python
1def safe_float_list(values):
2    result = []
3    for value in values:
4        if value is None:
5            continue
6        try:
7            result.append(float(value))
8        except ValueError:
9            continue
10    return result

Convert in place only when you really want mutation

Sometimes you want to overwrite the original list instead of creating a new one:

python
1values = ["1.2", "3", "4.75"]
2values[:] = [float(value) for value in values]
3
4print(values)

Using slice assignment keeps the same list object but replaces its contents. That matters only if other parts of the program already hold a reference to the same list.

In most normal code, returning a new list is simpler and safer.

Be careful with locale and formatting

float() expects standard Python numeric strings such as "3.14" or "1e6". It does not automatically handle all human-friendly formats. For example:

  • '"1,234.56" fails because of the comma.'
  • '"3,14" fails if the comma is meant as a decimal separator.'
  • '"$12.50" fails because of the currency symbol.'

If you need to parse formatted strings, normalize them first:

python
raw = ["1,234.56", "99.95"]
numbers = [float(value.replace(",", "")) for value in raw]
print(numbers)

The exact cleanup rule depends on the input format. The important part is not to assume float() can interpret every string that looks vaguely numeric to a human.

Common Pitfalls

The biggest mistake is assuming every item in the list is convertible. One bad value raises an exception and stops the whole process.

Another common issue is forgetting that map in modern Python returns an iterator, not a list. If you need a list, wrap it with list(...).

People also mutate the original list accidentally when a new list would be safer. Use in-place replacement only when you actually need to preserve the original list reference.

Finally, formatted strings with commas, currency symbols, or locale-specific decimal separators need preprocessing before conversion.

Summary

  • Use [float(x) for x in values] as the normal Python solution.
  • 'list(map(float, values)) is also valid if you prefer that style.'
  • Add try and except handling when the data may contain invalid entries.
  • Convert in place only if you intentionally want to mutate the existing list.
  • Clean formatted numeric strings before calling float().

Course illustration
Course illustration

All Rights Reserved.