How to solve TypeError: can only concatenate list (not “int”) to list

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…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to solve TypeError: can only concatenate list (not “int”) to list." Deven | Sciencx - Monday March 15, 2021, https://www.scien.cx/2021/03/15/how-to-solve-typeerror-can-only-concatenate-list-not-int-to-list/
HARVARD
Deven | Sciencx Monday March 15, 2021 » How to solve TypeError: can only concatenate list (not “int”) to list., viewed ,<https://www.scien.cx/2021/03/15/how-to-solve-typeerror-can-only-concatenate-list-not-int-to-list/>
VANCOUVER
Deven | Sciencx - » How to solve TypeError: can only concatenate list (not “int”) to list. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/15/how-to-solve-typeerror-can-only-concatenate-list-not-int-to-list/
CHICAGO
" » How to solve TypeError: can only concatenate list (not “int”) to list." Deven | Sciencx - Accessed . https://www.scien.cx/2021/03/15/how-to-solve-typeerror-can-only-concatenate-list-not-int-to-list/
IEEE
" » How to solve TypeError: can only concatenate list (not “int”) to list." Deven | Sciencx [Online]. Available: https://www.scien.cx/2021/03/15/how-to-solve-typeerror-can-only-concatenate-list-not-int-to-list/. [Accessed: ]
rf:citation
» How to solve TypeError: can only concatenate list (not “int”) to list | Deven | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.