Last updated: Apr 8, 2024
Reading timeยท5 min
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.
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.
One way to solve the error is to access the list at a specific index and pass
the item to the float()
class.
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.
0
based, so the first item in the list has an index of 0
, and the last has an index of -1
.If you meant to convert all items in the list to floating-point numbers, use a list comprehension.
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.
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.
The last step is to convert the map object to a list.
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.
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.
The most common sources of None
values are:
None
implicitly).None
.One way to solve the error is to provide a fallback value, e.g. 0
if the
variable stores None
.
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 explicitly return a value return None
.
# ๐๏ธ 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.
def get_str(): return '3.14' result = float(get_str()) print(result) # ๐๏ธ 3.14
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.
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.
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
.
Another common cause of the error is having a function that returns a value only if a condition is met.
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
.
None
.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.
def get_num(a): if a > 15: return a return 0 # ๐๏ธ Return a 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 of whether the condition is met.
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.
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 the method
m.get_str()
, so our code actually tries to convert a method to a floating-point number.To solve the error, make sure to call the method.
class MyClass(): def get_str(self): return '3.14' e = MyClass() # โ Call the 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.
my_obj.my_method(10, 20)
.If you aren't sure what type of object a variable stores, use the type()
class.
class MyClass(): def get_str(self): return '3.14' e = MyClass() print(type(e.get_str)) # ๐๏ธ <class 'method'> print(callable(e.get_str)) # ๐๏ธ True my_float = 3.14 print(type(my_float)) # ๐๏ธ <class '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.