Last updated: Apr 8, 2024
Reading timeยท5 min
The Python "TypeError: int() argument must be a string, a bytes-like object or
a real number, not 'NoneType'" occurs when we pass a None
value to the int()
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: int() argument must be a string, a bytes-like object or a real number, not 'NoneType' result = int(example)
We are passing a None
value to the
int() 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 = int(example or 0) print(result) # ๐๏ธ 0
If the variable stores None, then 0
gets returned from the expression.
Functions that don't explicitly return a value return None
.
# ๐๏ธ this function returns None def get_str(): print('100') # โ๏ธ TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType' result = int(get_str())
You can use a return statement to return a value from a function.
def get_str(): return '100' result = int(get_str()) print(result) # ๐๏ธ 100
The function now returns a string that we can convert to an integer.
Use an if
statement if you need to
check whether a variable doesn't store a None value
before passing it to the int()
class.
example = None if example is not None: result = int(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 = int(example) print(result) # ๐๏ธ 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) 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 fallback if condition not met my_num = get_num(10) print(my_num) # ๐๏ธ 0
Now the function is guaranteed to return a value regardless if the condition is met.
The Python "TypeError: int() argument must be a string, a bytes-like object or
a real number, not 'list'" occurs when we pass a list to the int()
class.
To solve the error, access a specific item in the list and pass the item to
the int()
class, e.g. int(my_list[0])
.
Here is an example of how the error occurs.
my_list = ['3', '5', '8'] # โ๏ธ TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list' result = int(my_list)
We passed an entire list to the int()
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 int()
class.
my_list = ['3', '5', '8'] result = int(my_list[0]) print(result) # ๐๏ธ 3
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 join all the items in the list into a string and convert the
result to an integer, use the join()
method.
my_list = ['3', '5', '8'] my_str = ''.join(my_list) print(my_str) # ๐๏ธ '358' my_int = int(my_str) print(my_int) # ๐๏ธ 358
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
If your list contains numbers or other types, convert all of the values to
string before calling join()
.
my_list = ['3', 5, 8] my_str = ''.join(map(str, my_list)) print(my_str) # ๐๏ธ '358' my_int = int(my_str) print(my_int) # ๐๏ธ 358
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
You can also use a list comprehension if you need to convert all items in a list to integers.
my_list = ['3', '5', '8'] new_list = [int(x) for x in my_list] print(new_list) # ๐๏ธ [3, 5, 8]
We pass each string in the list to the int()
class to convert each item to an
integer.
You can learn more about the related topics by checking out the following tutorials: