TypeError: int() Argument Must Be a String or a Number, Not 'List' - Solutions & Tips for Python Programmers

As a Python programmer, you might have come across the TypeError: int() argument must be a string or a number, not 'list' error. This error occurs when you try to convert a list to an integer using the int() function, which is not allowed in Python.

In this guide, we will explore the reasons behind this error and provide step-by-step solutions to fix it. Additionally, we will answer some frequently asked questions related to this topic.

Table of Contents

Understanding the Error

The int() function in Python is used to convert a value to an integer. However, this function accepts only strings and numbers as input arguments. When you try to pass a list object to the int() function, Python raises the TypeError: int() argument must be a string or a number, not 'list' error.

For example, the following code will result in this error:

my_list = [1, 2, 3]
integer_value = int(my_list)

Solutions

To resolve this error, you need to convert each element of the list to an integer instead of trying to convert the entire list. This can be done using various methods, as explained below.

Solution 1: Convert Each Element of the List to an Integer

Use a for loop to iterate through the list and convert each element to an integer using the int() function.

my_list = ['1', '2', '3']
integer_list = []

for element in my_list:
    integer_list.append(int(element))

print(integer_list)

Solution 2: Use List Comprehension

List comprehension is a concise way to create a new list by applying an expression to each element of an existing list.

my_list = ['1', '2', '3']
integer_list = [int(element) for element in my_list]

print(integer_list)

Solution 3: Use the Map Function

The map() function applies a given function to all items of an iterable (e.g., list) and returns a new iterable (e.g., list) of the results.

my_list = ['1', '2', '3']
integer_list = list(map(int, my_list))

print(integer_list)

FAQs

Q1: Why does the int() function not accept lists as input?

The int() function is designed to convert a single value to an integer. Lists contain multiple values, and converting an entire list to a single integer doesn't make logical sense. Instead, you should convert each element of the list individually.

Q2: How can I handle non-integer values in the list?

You can use exception handling to skip non-integer values and continue with the next element in the list. For example:

my_list = ['1', '2', '3', 'a']
integer_list = []

for element in my_list:
    try:
        integer_list.append(int(element))
    except ValueError:
        pass

print(integer_list)

Q3: How can I convert a nested list to a list of integers?

You can use nested list comprehension or a nested loop to convert each element of the nested list to an integer. For example:

nested_list = [['1', '2'], ['3', '4']]
integer_list = [int(element) for sublist in nested_list for element in sublist]

print(integer_list)

Q4: Can I use the float() function to convert a list to a list of floats?

Similar to the int() function, the float() function also doesn't accept the list as an input argument. You need to use the same methods discussed above for the int() function but replace int() with float().

Q5: How do I convert a list of integers to a list of strings?

You can use list comprehension or the map() function to convert a list of integers to a list of strings. For example:

my_list = [1, 2, 3]
string_list = [str(element) for element in my_list]

# or

string_list = list(map(str, my_list))

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.