How To Fix the Python Error: TypeError: Int() Argument Must Be a String, a Bytes-Like Object or a Number, Not ‘List’

Int is an incredibly useful part of the Python language. It gives you the ability to manipulate and verify numerical data in a variety of different ways. But it does place some limitations on which data types you can work with. When there’s a conflict in data type after calling Int, you’ll receive a variety of error messages. One of the most common being ” typeerror: int() argument must be a string, a bytes-like object or a number, not ‘list’ “. This Python error is fairly easy to fix. But it’s important to first understand why it comes up in the first place.

What Does the Error Mean?

In simplest terms, the error simply means that you’ve passed a Python list to the Int method. Int will convert any compatible data type into an integer. But it can’t work directly with lists. This error commonly occurs when someone wants to send one or more items within a list to Int but mistakenly passes the list variable in its entirety as the argument.

A Deeper Look at Python’s Int

This TypeError might make it seem like the Int method is overly picky. But that’s far from the truth. Consider the following Python code.

varList = [False,999,99.9,”998″,b”997″]

for x in varList:
    print(“__________”)
    print(x)
    print(type(x))
    print(int(x))

We begin by creating a list, varList, that contains values corresponding to a wide variety of different data types. We then iterate through varList and print out some basic information about each item. This shows the item’s value, data type, and what happens when it’s passed to Int. The loop exits cleanly despite the fact that we’re using a wide variety of different data types. However, things will change with the addition of a single extra line. Use the same code, but append this line at the end.

print( int(varList) )

This line triggers the TypeError. Taken as a whole, this example shows the most common cause for this particular error message. The error usually results from passing a list containing the values we want to work with. In contrast to sending an individual data point from within the list.

How To Fix the Error

Fixing this error is largely dependent on the underlying cause. We’ve touched on the most likely explanation for the error – passing a list instead of a value stored within it. These cases simply require us to fix the typo responsible for the problem. For example, we’d change the previous example line into this fixed version.

print( int(varList[0]) )

The 0 would of course change to whichever position in the list was appropriate to our needs. We could also extend this concept to check for lists before passing a variable to Int. For example, the following is a simple way to get around the problem.

def processOurList(preProcList):
    if isinstance(preProcList, list):
        print(“List detected, sending default value”)
        return 0
    else:
        print(“Not a list”)
        return int(preProcList)

varList = [False,999,99.9,”998″,b”997″]

print(processOurList(varList[3]))
print(processOurList(varList))

In this example, we create a processOurList function which checks the argument’s type before processing it. If the argument is a list, the function will just return a 0. If the argument isn’t a list, then the function will send it to Int and return the result. We initially call the function by sending the 4th item in the varList as an argument. The script processes the result cleanly. And the next time the function is called we send the entire list. This time the function detects the list and cleanly returns a 0 integer.

Of course, you’d need to tailor this concept to your individual code in a real-world situation. Fixing the error is important. But it’s equally important to find out why the circumstances which gave rise to the error came about. There should never be any confusion about the variable types you’re working with at any given moment.

How To Fix the Python Error: TypeError: Int() Argument Must Be a String, a Bytes-Like Object or a Number, Not ‘List’
Scroll to top