How to solve TypeError: can only concatenate list (not “int”) to list
Posted on: March 15, 2021 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
Share on social media
//