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

One of Python’s greatest strengths comes from its various data types. The language provides a considerable amount of flexibility in using or even converting data from one format to another. However, if you’ve received an error message reading ‘ int argument must be a string a bytes-like object or a number not list ‘ then you know that’s not an absolute rule. Python’s flexible with data types, but it does impose some restrictions. But this error can be solved by looking a little deeper into the problem.

What Does the Error Message Mean?

The Python error results from use of the Int function. Int is used to convert whatever argument has been passed to it into an integer. It’s commonly used to convert something that refers to a number, like strings or bytes-like objects containing a number, into an actual integer. For example, you could convert a “4” string into a 4 integer. If this error arises it’s typically due to a Python list rather than a number-like value, being passed to Int.

A Deeper Look Into the Problem

The error is a fairly straightforward problem caused by issues that are often a little less self-apparent. We can begin understanding the issue by looking at Python code which demonstrates this problem in its simplest form.

numStringList = [‘1′,’2′,’3′,’4′,’5’]
numString = ‘999’
numStringConvert = int(numString)
print( numStringConvert )
numListConvert = int(numStringList)
print(numListConvert)

The code begins by creating a Python list with numbers ranging from 1 to 5 in string format. We then create a variable called numString that contains 999 in string format. Next, we assign the result of Int to a variable called numStringConvert. Note that the Int argument is our 999 string. We then print out the result of that conversion. It will run cleanly and output 999. If we passed numStringConvert to type, the function output would show it to be of class int.

We then try to assign the result of passing numStringList to int into the numListConvert variable. If it worked, we’d see a new integer printed out on the next line. However, the script will exit with the “int() argument must be a string, a bytes-like object or a number, not ‘list'” error. The problem is easy to see in this simple example. A list was passed to Int, rather than just a single value from within that list. This can be further illustrated by the following example.

numStringList = [‘999′,’998′,’997′,’996′,’995’]
numStringSubConvert = int(numStringList[0])
print( numStringSubConvert )
numListConvert = int(numStringList)
print(numListConvert)

This example is similar to the first. Except here we are able to convert one value from the list into an integer. This is because we specify a position in the list which points to the string in the [0] position. The script prints the result, and then exits with the usual error when we try to run Int on the entire list.

The problem can also arise from the use of a nested list. Python’s interpreter gives us a tremendous amount of freedom in adding containers into other containers. We can easily create a list that has another list nested within it. Consider the following example.

numStringList = [‘999′,’998′,’997′,’996′,’995’]
numStringNest = [numStringList, ‘2’, ‘3’, ‘4’, ‘5’]
print(numStringNest[0][0])
numStringSubConvert = int(numStringNest[0])
print( numStringSubConvert )

This code offers a variation on the familiar problem we’ve seen a few times so far. We’ve created a list, numStringNest, that contains another list of numbers formatted as strings. Printing the [0][0] position of numStringNest shows how we can grab the 999 string. But if we only used one [0] Python will pass a list to Int rather than the 999 value contained within that list. And, as we see, the script exits once again with the usual error.

How To Solve the Error

So far this seems like an easy problem to fix. You simply make sure that you’re actually passing a single value, rather than a list or other container, to Int. And it’s true that the most common source of this error simply arises from forgetting to specify specific list elements. So, for the previous example, we’d simply need to modify it thusly.

numStringList = [‘999′,’998′,’997′,’996′,’995’]
numStringNest = [numStringList, ‘2’, ‘3’, ‘4’, ‘5’]
numStringSubConvert = int(numStringNest[0][0])
print(numStringSubConvert)
print(type(numStringSubConvert))

In this example, we simply make sure to specify the correct position within the substring when passing numStringNest to Int. It’s a simple fix to a simple issue that’s most commonly the result of a typo or overlooked assignment. It’s also important to take special note of something that doesn’t trigger an error. Consider the following variation on this code.

numStringList = [b’999′,’998′,’997′,’996′,’995′]
numStringNest = [numStringList, ‘2’, ‘3’, ‘4’, ‘5’]
numStringSubConvert = int(numStringNest[0][0])
print(type(numStringNest[0][0]))
print(numStringSubConvert)
print(type(numStringSubConvert))

The initial error mentions that a bytes-like object can be passed to Int. And this is indeed one of the instances where Python’s interpreter doesn’t mind if we interchange strings and bytes-like objects. The only thing that matters is that Int receives something like a number among the required fields. In this variation on our code, we declare 999 with a b in front of it. This special notation defines it as a bytes-like object. We go about a similar process of passing it to Int. But note the type printing. It shows that we began with 999 as a bytes object. But it was successfully transformed into an integer when passed to Int.

However, fixing this error becomes a little more complicated if the underlying issue goes beyond simply misidentifying a position within our list. What if we had wanted to convert all of the numerical strings into a single integer? That can be done fairly easily, as seen below.

numStringList = [‘1′,’2′,’3′,’4′,’5’]
numListConvert = int(”.join(numStringList))
print(type(numListConvert))
print(numListConvert)

The magic happens in the second line where we create an empty string and join it to the numStringList. Join is a method of the strings class, so we need to first create an empty instance of one. Join then iterates over every element within the list and passes it as a whole to Int. Of course, this loses the list formatting. We could keep those values as a list, but with integers instead of strings, by performing the following procedure.

numStringList = [‘1′,’2′,’3′,’4′,’5’]
numListConvert = [int(x) for x in numStringList]
print(type(numListConvert[0]))
print(numListConvert)

We simply use list comprehension to pass all of the items in numStringList as we iterate through them. This recreates the list as integers within numListConvert. The final print statements show that the type of the first item is an integer and that we’re looking at a standard list. The main point to keep in mind here is that we always ensure that we’re passing an item in the list rather than the list itself.

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