close

[Solved] TypeError: can only concatenate list (not “str”) to list

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: can only concatenate list (not “str”) to list in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: can only concatenate list (not “str”) to list Error Occurs?

Today I get the following error TypeError: can only concatenate list (not “str”) to list in python.

How To Solve TypeError: can only concatenate list (not “str”) to list Error ?

  1. How To Solve TypeError: can only concatenate list (not “str”) to list Error ?

    To Solve TypeError: can only concatenate list (not “str”) to list Error Means you're trying to concatenate a list and a string. To add an item to a list, use the list.append() method.

  2. TypeError: can only concatenate list (not “str”) to list

    To Solve TypeError: can only concatenate list (not “str”) to list Error Means you're trying to concatenate a list and a string. To add an item to a list, use the list.append() method.

Solution 1

I think what you want to do is add a new item to your list, so you have change the line newinv=inventory+str(add) with this one:

newinv = inventory.append(add)

What you are doing now is trying to concatenate a list with a string which is an invalid operation in Python.

However I think what you want is to add and delete items from a list, in that case your if/else block should be:

if selection=="use":
    print(inventory)
    remove=input("What do you want to use? ")
    inventory.remove(remove)
    print(inventory)

elif selection=="pickup":
    print(inventory)
    add=input("What do you want to pickup? ")
    inventory.append(add)
    print(inventory)

You don’t need to build a new inventory list every time you add a new item.

Solution 2

That’s not how to add an item to a string. This:

newinv=inventory+str(add)

Means you’re trying to concatenate a list and a string. To add an item to a list, use the list.append() method.

inventory.append(add) #adds a new item to inventory
print(inventory) #prints the new inventory

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read