How to Solve Python TypeError: float() argument must be a string or a number, not ‘list’

by | Programming, Python, Tips

You cannot convert a list to a floating-point number. If you try to pass a list as an argument to the built-in float() method, you will raise the TypeError: float() argument must be a string or a number, not ‘list’.

If you want to convert the elements of a list to floats, you can use the map() function. For example,

a_list = list(map(float, a_list))

We can also use list comprehension to create a new list of floating-point numbers.

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: float() argument must be a string or a number, not ‘list’

A TypeError occurs when you perform an operation with an invalid data type. The built-in float() method accepts a string or a number. If you pass a list to the float() method, this is an invalid data type and will raise the TypeError.

Example

Let’s look at an example where we have a list of numeric strings. We want to convert the list to a list of floating-point numbers.

a_list = ["2.1", "5.4", "0.7", "0.04", "1.0"]

float_list = float(a_list)

print(float_list)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 3>()
      1 a_list = ["2.1", "5.4", "0.7", "0.04", "1.0"]
----> 3 float_list = float(a_list)
      5 print(float_list)

TypeError: float() argument must be a string or a number, not 'list'

The error occurs because we passed a list to the float() method, which is an invalid data type to convert to a floating-point number.

Solution #1: Use the map() function

We can use the built-in map function to solve this error. The map function returns an iterator that applies a function to every item of an iterable. In our case, the function we want to apply is float() and the iterable is our list a_list. Applying the float() function to each element in the list is possible because the elements are strings.

The map() function returns a map object which is an iterator. We can convert the map object to a list using the list() function.

Let’s look at the revised code:

a_list = ["2.1", "5.4", "0.7", "0.04", "1.0"]

float_list = list(map(float, a_list))

print(float_list)
[2.1, 5.4, 0.7, 0.04, 1.0]

We successfully converted the list of strings to a list of floats.

Solution #2: Use List Comprehension

List comprehension provides a way to create a new list based on the values of an existing list. We can use list comprehension to convert a list of strings to a list of floating-point numbers. Let’s look at the revised code:

a_list = ["2.1", "5.4", "0.7", "0.04", "1.0"]

float_list = [float(x) for x in a_list]

print(float_list)

Let’s run the code to get the result:

[2.1, 5.4, 0.7, 0.04, 1.0]

If we want to convert an individual element, we can use the subscript operator [] to get individual elements of the list. For example,

number = float(a_list[0])

print(number)
2.1

Summary

Congratulations on reading to the end of this tutorial! The TypeError: float() argument must be a string or a number, not ‘list’ occurs if you try to convert a list object to a floating-point number. We can apply the map() function to apply float() to each element in the list, or we can use list comprehension.

For further reading on converting values using built-in functions, go to the article: How to Solve Python TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’

For further reading on TypeErrors go to the article:

How to Solve Python TypeError: int() argument must be a string, a bytes-like object or a number, not ‘NoneType’
Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨