Python beginners frequently encounter the error message stating you can only concatenate list, not int to list. This error pops up when you try to combine a list with an integer using the plus sign. It stops your code cold, but fixing it is straightforward once you understand the cause.
You might be building a list of numbers and accidentally try to add a single integer directly. Python lists are strict about what you can concatenate with them. The plus operator only works between two lists, not between a list and a number.
Let’s break down exactly why this happens and how to solve it. We’ll cover common scenarios, debugging tricks, and best practices to avoid this error in the future.
Can Only Concatenate List Not Int To List
The core issue is a type mismatch. Python’s list concatenation using the + operator requires both operands to be lists. When you try to add an integer, Python raises a TypeError because it doesn’t know how to merge a number into a list automatically.
Here’s a simple example that triggers the error:
my_list = [1, 2, 3]
my_list + 4 # This line causes the error
Python sees [1, 2, 3] + 4 and says “I can only concatenate list, not int to list.” The interpreter expects another list on the right side of the plus sign, not a plain integer.
Why Python Enforces This Rule
Python is a dynamically typed language, but it still has strong type checking for operations like concatenation. Lists are sequences, and the + operator is defined to join two sequences of the same type. Integers are not sequences, so the operation fails.
This design prevents ambiguous behavior. If Python allowed list + int, it would have to guess whether you want to append the integer, add it to each element, or something else. By raising an error, Python forces you to be explicit about your intention.
Common Scenarios That Trigger The Error
You’ll see this error in several typical coding situations. Here are the most frequent ones:
- Appending a single number to a list using
+instead of.append() - Building a list incrementally in a loop and accidentally adding an integer
- Mixing up variable types when concatenating multiple lists
- Forgetting to wrap an integer in square brackets before concatenation
- Using the wrong operator for list extension, like
+instead of+=with a list
Each of these scenarios has a simple fix. Let’s go through them one by one.
How To Fix The Error
There are three main ways to resolve the “can only concatenate list not int to list” error. Choose the method that best matches your goal.
Method 1: Wrap The Integer In A List
If you want to add a single integer to the end of an existing list, wrap it in square brackets to make it a list of one element:
my_list = [1, 2, 3]
my_list + [4] # Now it works: [1, 2, 3, 4]
This tells Python you want to concatenate two lists. The integer 4 becomes a list containing just that number. This is the most direct fix for the concatenation error.
Method 2: Use The Append Method
If your goal is to add a single item to the list, use the .append() method instead of concatenation:
my_list = [1, 2, 3]
my_list.append(4) # my_list becomes [1, 2, 3, 4]
The .append() method modifies the original list in place and accepts any data type, including integers. It does not return a new list, so you don’t need to assign the result.
Method 3: Use The Extend Method For Multiple Items
When you have multiple integers to add, use the .extend() method with a list of those integers:
my_list = [1, 2, 3]
my_list.extend([4, 5, 6]) # my_list becomes [1, 2, 3, 4, 5, 6]
This method also modifies the list in place and accepts any iterable. It’s more efficient than repeatedly appending in a loop.
Debugging The Error In Real Code
When you encounter this error in a larger program, the traceback tells you exactly which line caused it. Look at the line number and check the operation that uses + between a list and something else.
Here’s a typical traceback:
Traceback (most recent call last):
File "example.py", line 5, in
result = my_list + number
TypeError: can only concatenate list (not "int") to list
The error message even tells you the type of the second operand, which is “int” in this case. This helps you identify the variable that needs to be converted or handled differently.
Common Debugging Steps
- Identify the line where the error occurs from the traceback
- Check the data types of both operands using
type() - Decide whether you need to wrap the integer in a list, use
.append(), or restructure your code - Test the fix with a small example to confirm it works
Let’s look at a more complex example where this error might appear in a loop:
numbers = [1, 2, 3]
new_numbers = []
for i in range(3):
new_numbers = new_numbers + i # Error here
In this loop, i is an integer, not a list. The fix is to either wrap i in a list or use .append():
# Fix 1: Wrap in list
for i in range(3):
new_numbers = new_numbers + [i]
# Fix 2: Use append
for i in range(3):
new_numbers.append(i)
Both approaches work, but .append() is generally more efficient for building a list incrementally.
Preventing The Error In The Future
Good coding practices can help you avoid this error altogether. Here are some strategies:
- Always check the data type of variables before concatenation
- Use type hints in your function signatures to clarify expected types
- Prefer
.append()or.extend()over+for adding items to a list - When using
+, ensure both operands are lists - Write unit tests that cover edge cases with mixed types
Using Type Hints
Python’s type hints can catch potential issues before runtime. For example:
def combine_lists(list1: list, list2: list) -> list:
return list1 + list2
This makes it clear that both arguments should be lists. If you accidentally pass an integer, a type checker like mypy will warn you.
Understanding The Plus Operator With Lists
The + operator for lists creates a new list that contains all elements from both original lists. It does not modify the original lists. This is different from .extend(), which modifies the list in place.
Here’s a comparison:
a = [1, 2]
b = [3, 4]
# Using +
c = a + b # c is [1, 2, 3, 4], a and b unchanged
# Using extend
a.extend(b) # a becomes [1, 2, 3, 4], b unchanged
Understanding these differences helps you choose the right approach for your specific use case.
Advanced Scenarios And Edge Cases
Sometimes the error appears in less obvious situations. Let’s explore a few advanced cases.
Nested Lists
If you have a list of lists and try to concatenate with an integer, the same error occurs:
matrix = [[1, 2], [3, 4]]
matrix + 5 # Error
The fix is the same: wrap the integer in a list, even if it’s a list of lists:
matrix + [5] # Works, but note: [5] is not a nested list
If you want to add a new row to the matrix, you need to wrap it properly:
matrix + [[5, 6]] # Adds a new row
Using Variables With Unknown Types
When working with user input or data from external sources, you might not know the type of a variable. Always validate or convert before concatenation:
user_input = input("Enter a number: ")
my_list = [1, 2, 3]
# Convert to integer if needed, then wrap in list
try:
number = int(user_input)
my_list = my_list + [number]
except ValueError:
print("Invalid input")
This prevents the error by ensuring the variable is an integer before wrapping it in a list.
List Comprehension Pitfalls
List comprehensions can also trigger this error if you’re not careful:
# This works fine
result = [x + 1 for x in [1, 2, 3]]
# But this causes an error if you try to concatenate
result = [1, 2, 3] + [x for x in range(3)] # Works because both are lists
The key is to always check that both sides of the + operator are lists.
Alternatives To Concatenation
Depending on your goal, there might be better alternatives to list concatenation.
Using The Plus Equals Operator
The += operator works similarly to + but modifies the list in place:
my_list = [1, 2, 3]
my_list += [4] # my_list becomes [1, 2, 3, 4]
Note that += also requires the right operand to be a list. Using my_list += 4 would still cause the error.
Using The Insert Method
If you need to add an integer at a specific position, use .insert():
my_list = [1, 2, 3]
my_list.insert(0, 0) # my_list becomes [0, 1, 2, 3]
This method takes an index and a value, and it works with any data type.
Using Slicing Assignment
For more complex operations, you can use slice assignment:
my_list = [1, 2, 3]
my_list[1:1] = [10] # Insert 10 at index 1: [1, 10, 2, 3]
This also requires the right side to be a list, but it gives you fine-grained control over where elements are added.
Frequently Asked Questions
Why does Python say “can only concatenate list not int to list”?
This error occurs because you’re trying to use the + operator between a list and an integer. Python only allows list concatenation with another list. The integer must be wrapped in a list first, or you should use .append() instead.
How do I fix “TypeError: can only concatenate list not int to list”?
You have three main options: wrap the integer in square brackets (e.g., my_list + [4]), use the .append() method (e.g., my_list.append(4)), or use .extend() for multiple integers (e.g., my_list.extend([4, 5])).
Can I concatenate a list with a float?
No, the same error occurs with floats or any non-list type. You must wrap the float in a list or use .append(). The error message will say “can only concatenate list not float to list” in that case.
What’s the difference between + and .append() for lists?
The + operator creates a new list containing elements from both original lists, leaving the originals unchanged. The .append() method adds a single element to the end of the existing list, modifying it in place and returning None.
How do I add multiple integers to a list without the error?
Use the .extend() method with a list of integers, or use += with a list on the right side. For example: my_list.extend([4, 5, 6]) or my_list += [4, 5, 6].
Summary And Best Practices
The “can only concatenate list not int to list” error is one of the most common type errors in Python. It’s easy to fix once you understand the underlying cause. Always remember that list concatenation requires two lists, not a list and a number.
Here are the key takeaways:
- Use
list + [integer]to concatenate a single integer - Use
list.append(integer)to add a single item - Use
list.extend([int1, int2])to add multiple items - Check variable types with
type()when debugging - Prefer methods like
.append()and.extend()for modifying lists
With these strategies, you’ll avoid this error and write cleaner, more robust Python code. Practice by intentionally creating and fixing this error in small scripts to build muscle memory. Over time, you’ll spot the pattern instantly and apply the correct fix without thinking twice.