Typeerror: can only concatenate list not int to list

As a Python programmer, one of the common error you may encounter is TypeError: can only concatenate list (not “int”) to list.

This error message usually occur when you are trying to concatenate a list and an integer using the “+” operator, yet this operation doesn’t allowed in Python.

In this article, we will show you on how to fix and provide an examples that causes an error.

Why does this error occur?

This error occur because of some common reasons , which are the following:

  • You are trying to add an integer to a list.
  • You are trying to add a list to an integer.
  • You are trying to add two lists together, but one of them contains an integer.

How the error occurs?

Here are the examples on how the error occurs:

We assume that we have a list that consist of an integer numbers, then we want to add one integer to each of them.

example_numbers = [1, 2, 3, 4, 5]
my_new_numbers = example_numbers + 1

Output:

Traceback (most recent call last):
File “main.py”, line 2, in
my_new_numbers = example_numbers + 1
TypeError: can only concatenate list (not “int”) to list

This error typically occurs because you are trying to add one integer to a list (numbers). We cannot concatenate an integer into a list like in the example code above.

How to Solve this Error can only concatenate list not int to list?

Here are the following solutions to solve the error can only concatenate list not int to list.

Solution 1: Using a Loop to Iterate over the List

The first way to solve the error is to use a loop to iterate over the list and add 1 to each element.

For example:

example_integers = [1, 2, 3, 4, 5]
my_new_numbers = []
for number in example_integers:
    my_new_numbers.append(number + 1)
print(my_new_numbers)

Output:

[2, 3, 4, 5, 6]

Solution 2: Initialize total to 0 before the loop

We assume that we have a list of numbers and we want to add them all together.

For example:

example_integer = [1, 2, 3, 4, 5]
for number in example_integer:
    total = total + number

This code will result in the “TypeError: can only concatenate list (not “int”) to list” error because we are trying to add a list (numbers) to an uninitialized variable (total).

To solve this error, we need to initialize total to 0 before the loop:

Let’s take a look at example:

example_integer = [1, 2, 3, 4, 5]
total = 0
for number in example_integer:
    total = total + number
    print(total)

Output:

1
3
6
10
15

Solution 3: Convert the Integer to a List before Adding it to the Other List

We are trying to add these three variables together using the “+” operator.

For example:

example_list1 = [1, 2, 3]
example_list2 = [4, 5, 6, 7, 8]
example_list3 = 9

result = example_list1 + example_list2 + example_list3
print(result)

However, when we try to add list3 to the other two lists, we get a TypeError because we can only concatenate lists together using the “+” operator, not an integer and a list.

To solve the error, we need to convert the Integer to a List before Adding it to the Other List.

Let’s take a look at the example:

example_list1 = [1, 2, 3]
example_list2 = [4, 5, 6, 7, 8]
example_list3 = [9]

result = example_list1 + example_list2 + example_list3
print(result)

Through converting example_list3 to a list containing only the integer 9, we can now concatenate it with the other two lists.

Which is the example_list1 and example_list2 using the “+” operator, without getting a TypeError.

Outout:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

This is the result of concatenating example_list1, example_list2, and example_list3 together.

Solution 4: Calling the Append() Method of the List Object

The last solution to solve this error is to call the append() method of the list object.

The append() method is used to add an element to the end of a list, array, or other similar data structure.

example_list = [1, 2, 3, 4, 5]
example_list.append(6)
print(example_list)

In this example, the append() method is used to add the integer 6 to the end of the example_list variable.

The output will be like this:

[1, 2, 3, 4, 5, 6]

Additional Resources

Here are some helpful resources you need to check out to understand more about Python Typerror

Conclusion

The TypeError: can only concatenate list (not “int”) to list typically occurs if you are trying to add an integer to a list.

To fix this error, use a loop to iterate over the list, initialize total to 0 before the loop, convert the Integer to a List before Adding it to the Other List, and call the append() method of the list object.

I hope this article will be able to help you to solve the error.

FAQs

What does “Typeerror can only concatenate list not int to list” mean?

The “Typeerror: can only concatenate list not int to list” means that you are trying to combine a list and an integer using the “+” operator, which is only allowed for combining two lists.

Can this error happen in other programming languages besides Python?

Yes, similar errors can occur in other programming languages that use the “+” operator to combine lists.

Is there a way to prevent this error from happening in the first place?

Yes, by being mindful of the types of data you’re working with and making sure you’re using the “+” operator correctly.