This content originally appeared on CodeSource.io and was authored by Deven
In this article, you will learn how to solve TypeError: can only concatenate list (not “int”) to list Python error.
Let’s look at a code example that produces the same error.
fruits = ["mango and orange", "pineapple", "grapes and black grapes", "banana"]
buy_price = [100, 200, 300, 400]
fruits_bought = []
for s in range(0, len(fruits)):
if buy_price[s] > 100:
fruits_bought = fruits_bought + s
for o in fruits_bought:
print(fruits[o])
Output
Traceback (most recent call last):
File "<string>", line 6, in <module>
TypeError: can only concatenate list (not "int") to list
In order to solve TypeError: can only concatenate list (not “int”) to list Python error you have to use the append()
method to add an item to your list. consider the example below:
fruits = ["mango and orange", "pineapple", "grapes and black grapes", "banana"]
buy_price = [100, 200, 300, 400]
fruits_bought = []
for s in range(0, len(fruits)):
if buy_price[s] > 100:
fruits_bought.append(s)
for o in fruits_bought:
print(fruits[o])
output
pineapple
grapes and black grapes
banana
The post How to solve TypeError: can only concatenate list (not “int”) to list appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven

Deven | Sciencx (2021-03-15T16:42:35+00:00) How to solve TypeError: can only concatenate list (not “int”) to list. Retrieved from https://www.scien.cx/2021/03/15/how-to-solve-typeerror-can-only-concatenate-list-not-int-to-list/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.