int() argument must be a string or real number not X [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
5 min

banner

# Table of Contents

  1. int() argument must be a string or real number not NoneType
  2. int() argument must be a string or real number not, 'list'

# int() argument must be a string or real number not NoneType

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.

int argument must be string or real number not nonetype

Here is an example of how the error occurs.

main.py
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.

# Common sources of None in Python

The most common sources of None values are:

  1. Having a function that doesn't return anything (returns None implicitly).
  2. Explicitly setting a variable to None.
  3. Assigning a variable to the result of calling a built-in function that doesn't return anything.
  4. Having a function that only returns a value if a certain condition is met.

# Providing a fallback value if the variable stores None

One way to solve the error is to provide a fallback value, e.g. 0 if the variable stores None.

main.py
example = None result = int(example or 0) print(result) # ๐Ÿ‘‰๏ธ 0

provide fallback value if none

If the variable stores None, then 0 gets returned from the expression.

# Functions that don't return anything return None

Functions that don't explicitly return a value return None.

main.py
# ๐Ÿ‘‡๏ธ 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())

functions returning none

You can use a return statement to return a value from a function.

main.py
def get_str(): return '100' result = int(get_str()) print(result) # ๐Ÿ‘‰๏ธ 100

using return statement

The function now returns a string that we can convert to an integer.

# Checking if the variable doesn't store None before calling int()

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.

main.py
example = None if example is not None: result = int(example) print(result) else: # ๐Ÿ‘‡๏ธ this runs print('variable stores a None value')

check if variable does not store none

Alternatively, you can reassign the variable to a fallback value.

main.py
example = None if example is None: example = 0 result = int(example) print(result) # ๐Ÿ‘‰๏ธ 0

# Functions that return a value only 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.

main.py
def get_num(a): if a > 15: return a my_num = get_num(10) print(my_num) # ๐Ÿ‘‰๏ธ None

returning value only if condition met

The if block in the get_num function is only run if the passed-in number is greater than 15.

In all other cases, the function doesn't return anything and ends up implicitly returning 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.

main.py
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.

# int() argument must be a string or real number not, 'list'

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]).

int argument must be string or real number not nonetype

Here is an example of how the error occurs.

main.py
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.

# Accessing the list at an index

One way to solve the error is to access the list at a specific index and pass the item to the int() class.

main.py
my_list = ['3', '5', '8'] result = int(my_list[0]) print(result) # ๐Ÿ‘‰๏ธ 3
Indices are 0 based, so the first item in the list has an index of 0, and the last has an index of -1.

# Joining the items of the list into a string

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.

main.py
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().

main.py
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.

# Converting all of the items in the list to integers

You can also use a list comprehension if you need to convert all items in a list to integers.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev