- How to Solve Python TypeError: float() argument must be a string or a number, not ‘list’
- Table of contents
- TypeError: float() argument must be a string or a number, not ‘list’
- Example
- Solution #1: Use the map() function
- Solution #2: Use List Comprehension
- Summary
- Share this:
- Python argument string or list
- # Table of Contents
- # float() argument must be a string or a real number, not list
- # Access the list at a specific index to solve the error
- # Converting all values in a list to floating-point numbers
- # float() argument must be string or real number, not NoneType
- # Common sources of None in Python
- # Providing a default value if the variable is None
- # Functions that don’t return a value return None
- # Checking whether a variable doesn’t store a None value
- # A function that only returns a value if a condition is met
- # float() argument must be string or real number, not method
- # Call the method to solve the error
- # Checking what type the variable stores
How to Solve Python TypeError: float() argument must be a string or a number, not ‘list’
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.
Table of contents
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 () 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)
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:
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)
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’
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!
Share this:
Python argument string or list
Last updated: Feb 3, 2023
Reading time · 5 min
# Table of Contents
# float() argument must be a string or a real number, not list
The Python «TypeError: float() argument must be a string or a real number, not ‘list'» occurs when we pass a list to the float() class.
To solve the error, access a specific item in the list and pass the item to the float() class, e.g. float(my_list[0]) .
Here is an example of how the error occurs.
Copied!my_list = ['1.1', '2.2', '3.3'] # ⛔️ TypeError: float() argument must be a string or a real number, not 'list' result = float(my_list)
We passed an entire list to the float() class which caused the error.
# Access the list at a specific index to solve the error
One way to solve the error is to access the list at a specific index and pass the item to the float() class.
Copied!my_list = ['1.1', '2.2', '3.3'] result = float(my_list[0]) print(result) # 👉️ 1.1
We accessed the list at index 0 and used the float() class to convert the value to a floating-point number.
Indices are 0 based, so the first item in the list has an index of 0 , and the last has an index of -1 .
# Converting all values in a list to floating-point numbers
If you meant to convert all items in the list to floating-point numbers, use a list comprehension.
Copied!my_list = ['1.1', '2.2', '3.3'] new_list = [float(x) for x in my_list] print(new_list) # 👉️ [1.1, 2.2, 3.3]
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
We pass each string in the list to the float() class to convert each item to an integer.
Alternatively, you can use the map() function.
Copied!my_list = ['1.1', '2.2', '3.3'] new_list = list(map(float, my_list)) print(new_list) # 👉️ [1.1, 2.2, 3.3]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
We passed the float() class to map() , so the class gets passed each value in the list as an argument and converts it to a floating-point number.
# float() argument must be string or real number, not NoneType
The Python «TypeError: float() argument must be a string or a real number, not ‘NoneType'» occurs when we pass a None value to the float() class.
To solve the error, correct the assignment or provide a fallback value.
Here is an example of how the error occurs.
Copied!example = None # ⛔️ TypeError: float() argument must be a string or a real number, not 'NoneType' result = float(example)
We are passing a None value to the float() class which causes the error.
# Common sources of None in Python
The most common sources of None values are:
- Having a function that doesn’t return anything (returns None implicitly).
- Explicitly setting a variable to None .
- Assigning a variable to the result of calling a built-in function that doesn’t return anything.
- Having a function that only returns a value if a certain condition is met.
# Providing a default value if the variable is None
One way to solve the error is to provide a fallback value, e.g. 0 if the variable stores None .
Copied!example = None result = float(example or 0) print(result) # 👉️ 0.0
The expression checks if the example variable stores a falsy value, in which case 0 is returned.
# Functions that don’t return a value return None
Functions that don’t explicitly return a value return None .
Copied!# 👇️ this function returns None def get_str(): print('3.14') # ⛔️ TypeError: float() argument must be a string or a real number, not 'NoneType' result = float(get_str())
You can use a return statement to return a value from a function.
Copied!def get_str(): return '3.14' result = float(get_str()) print(result) # 👉️ 3.14
# Checking whether a variable doesn’t store a None value
Use an if statement if you need to check whether a variable doesn’t store a None value before passing it to the float() class.
Copied!example = None if example is not None: result = float(example) print(result) else: # 👇️ this runs print('variable stores a None value')
Alternatively, you can reassign the variable to a fallback value.
Copied!example = None if example is None: example = 0 result = float(example) print(result) # 👉️ 0.0
If the variable is equal to None , the if block runs and sets it to 0 .
# A function that only returns a value if a condition is met
Another common cause of the error is having a function that returns a value only if a condition is met.
Copied!def get_num(a): if a > 15: return a my_num = get_num(10.5) print(my_num) # 👉️ None
The if block in the get_num function is only run if the passed-in number is greater than 15 .
To solve the error, you either have to check if the function didn’t return None or return a default value if the condition is not met.
Copied!def get_num(a): if a > 15: return a return 0 # 👈️ return fallback if condition not met my_num = get_num(10.5) print(my_num) # 👉️ 0
Now the function is guaranteed to return a value regardless if the condition is met.
# float() argument must be string or real number, not method
The Python «TypeError: float() argument must be a string or a real number, not ‘method'» occurs when we pass a method to the float() class.
To solve the error, make sure to call the method with parentheses, e.g. my_method() .
Here is an example of how the error occurs.
Copied!class MyClass(): def get_str(self): return '3.14' m = MyClass() # ⛔️ TypeError: float() argument must be a string or a real number, not 'method' result = float(m.get_str) # 👈️ forgot to call method
We forgot to call the method with parentheses, e.g. m.get_str() , so our code actually tries to convert a method to a floating-point number.
# Call the method to solve the error
To solve the error, make sure to call the method.
Copied!class MyClass(): def get_str(self): return '3.14' e = MyClass() # ✅ call method() with parentheses result = float(e.get_str()) print(result) # 👉️ 3.14
We used parentheses to invoke the method, so now our code converts the return value of the method to a float.
If your method takes arguments, make sure to provide them when calling it, e.g. my_obj.my_method(10, 20) .
# Checking what type the variable stores
If you aren’t sure what type of object a variable stores, use the type() class.
Copied!class MyClass(): def get_str(self): return '3.14' e = MyClass() print(type(e.get_str)) # 👉️ print(callable(e.get_str)) # 👉️ True my_float = 3.14 print(type(my_float)) # 👉️ print(isinstance(my_float, float)) # 👉️ True
The type class returns the type of an object.
The isinstance function returns True if the passed-in object is an instance or a subclass of the passed-in class.
The callable function takes an object as an argument and returns True if the object appears callable, otherwise, False is returned.
If the callable() function returns True , it is still possible that calling the object fails, however, if it returns False , calling the object will never succeed.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.