Select Page

How to Solve Python TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’

by | Programming, Python, Tips

You cannot convert a list to an int. If you try to pass a list as an argument to the built-in int() method, you will raise the TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’.

If you want to convert the elements of a list to integers, you can use the map() function. For example,

a_list = list(map(int, a_list))

We can also use list comprehension to create a new list of integers.

This tutorial will go through the error in detail and how to solve it with code examples.


TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’

A TypeError occurs when you perform an operation with an invalid data type. The built-in int() method accepts a string, a bytes-like object, or a number. If you pass a list to the int() method, this is an invalid data type and will raise the TypeError.

Example

Let’s look at an example where we have a list of numerical strings. We want to convert the list to a list of integers.

a_list = ["2", "4", "6", "8", "10"]

int_list = int(a_list)

print(int_list)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [10], in <cell line: 3>()
      1 a_list = ["2", "4", "6", "8", "10"]
----> 3 int_list = int(a_list)
      5 print(int_list)

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

The error occurs because we passed a list to the int() method, which is an invalid data type to convert to an integer.

Solution #1: Use the map() function

We can use the built-in map function to solve this error. The map function returns an iterator that applies a function to every item of an iterable. In our case, the function we want to apply is int() and the iterable is our list a_list. Applying the int() function to each element in the list is possible because the elements are strings.

The map() function returns a map object which is an iterator. We can convert the map object to a list using the list() function.

Let’s look at the revised code:

a_list = ["2", "4", "6", "8", "10"]

int_list = list(map(int, a_list))

print(int_list)

Let’s run the code to get the result:

[2, 4, 6, 8, 10]

We successfully converted the list of strings to a list of integers.

Solution #2: Use List Comprehension

List comprehension provides a way to create a new list based on the values of an existing list. We can use list comprehension to convert a list of strings to a list of integers. Let’s look at the revised code:

a_list = ["2", "4", "6", "8", "10"]

int_list = [int(x) for x in a_list]

print(int_list)

Let’s run the code to get the result:

[2, 4, 6, 8, 10]

If we want to convert an individual element, we can use the subscript operator [] to get individual elements of the list. For example,

number = int(a_list[0])

print(number)
2

Summary

Congratulations on reading to the end of this tutorial! The TypeError: int() argument must be a string, a bytes-like object or a number, not ‘list’ occurs if you try to convert a list object to an integer. We can apply the map() function to apply int() to each element in the list, or we can use list comprehension.

For further reading on converting values using built-in functions, go to the articles:


Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!