Lists can be concatenated to other lists. This means you can add the contents of one list to another list. Values with other data types, such as integers, cannot be concatenated to a list.
If you try to concatenate an integer to a list, the Python interpreter returns a “TypeError: can only concatenate list (not “int”) to list” error.
In this guide, we talk about what this error means and how it works. We walk through an example to help you navigate fixing this issue in your code.
TypeError: can only concatenate list (not “int”) to list
Concatenation makes it easy to add two lists together. While you can use the extend() method to add a list to another list, concatenation only requires the use of one symbol: the plus sign (+).
Lists are not the only object which can be concatenated. Any iterable object, such as a dictionary or a tuple, can be concatenated.
Two objects of different data types cannot be concatenated. This means you cannot concatenate a list with a dictionary, or an integer with a list.
You encounter the “TypeError: can only concatenate list (not “int”) to list” if you use concatenation to add a single integer item to a list.
An Example Scenario
We’re going to build a program that tracks how many orders have been placed for particular sandwiches at a cafe on a Friday. We want to filter out all of the sandwiches that have been ordered more than 75 times so we can see what sandwiches are most popular.
We start by defining two lists: a list of sandwiches and a list containing how many orders were placed for each sandwich.
sandwiches = ["Egg and Cress", "Chicken Club", "Hummus and Tomato", "Cheese"] orders = [54, 77, 22, 98]
Our lists are assigned to the variables “sandwiches” and “orders”. Next, we define a list which tracks the index numbers of the sandwiches that have been ordered more than 75 times.
orders_75 = 
At the moment, these lists are empty. This is because we have not yet figured out which sandwiches have been ordered over 75 times.
Next, we iterate over our lists of sandwiches and orders to find out which sandwiches have been ordered more than 75 times. We can do this using a for loop:
for s in range(0, len(sandwiches)): if orders[s] > 75: orders_75 = orders_75 + s
Our code loops through each number in the range of 0 and the length of the “sandwiches” list. If the corresponding value in the “orders” list for a sandwich is greater than 75, we add the index position of that sandwich to the “orders_75” list.
Next, we write a for loop that prints out which sandwiches have been ordered more than 75 times to check if our program works:
for o in orders_75: print(sandwiches[o])
Let’s run our Python code and see what happens:
Traceback (most recent call last): File "main.py", line 9, in <module> orders_75 = orders_75 + s TypeError: can only concatenate list (not "int") to list
Our code returns an error.
Take a look at the line of code mentioned in our error message:
orders_75 = orders_75 + s
We’re trying to concatenate an integer value to our list. We cannot do this because concatenation only works on iterable objects of the same data type.
To solve this error, we must use the
append() method to add an item to our list. This method is specifically designed to add items to lists. Items can be of any data type, such as dictionaries, integers, or floating-point numbers.
Change our orders_75 line of code to use the
We do not need to assign any values to “orders_75” because the
append() method adds an item to a list in-place. Let’s execute our code:
Chicken Club Cheese
Our code prints a list of the sandwiches that have been ordered more than 75 times. There are two sandwiches that met this criteria: Chicken Club and Cheese.
The “TypeError: can only concatenate list (not “int”) to list” error is raised when you try to concatenate an integer to a list.
This error is raised because only lists can be concatenated to lists. To solve this error, use the
append() method to add an item to a list.
Now you’re ready to solve this Python error like a professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more