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 + numberThis 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
- Typeerror can only concatenate str not float to str
- Typeerror can only concatenate str not float to str
- Typeerror can only concatenate str not nonetype to str
Frequently Asked Questions
What is Python TypeError and what causes it?
TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.
How do I quickly debug a Python TypeError?
Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.
Should I catch TypeError or let it propagate?
For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.
How do I prevent TypeError in production?
Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.
Where can I find more TypeError fixes?
Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.
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
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.
Yes, similar errors can occur in other programming languages that use the “+” operator to combine lists.
Yes, by being mindful of the types of data you’re working with and making sure you’re using the “+” operator correctly.
