Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/What is the typeerror: can only concatenate list (not "str") to list problem, and how to solve it?
Next
Answered
Ali Brown
  • 30
Ali Brown
Asked: May 18, 20222022-05-18T18:00:52+00:00 2022-05-18T18:00:52+00:00In: python

What is the typeerror: can only concatenate list (not “str”) to list problem, and how to solve it?

  • 30

. Advertisement .

..3..

. Advertisement .

..4..

I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:

Traceback (most recent call last):
  File "H:/Year 10/Computing/A453/Python Programs/inventory.py", line 15, in <module>
  newinv=inventory+str(add)
 TypeError: can only concatenate list (not "str") to list

I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “typeerror: can only concatenate list (not “str”) to list” problem, pls let me know. Here is what I do:

inventory=["sword","potion","armour","bow"]
 print(inventory)
 print("\ncommands: use (remove) and pickup (add)")
 selection=input("choose a command [use/pickup]")
 
 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? ")
  newinv=inventory+str(add)
  print(newinv)

Thanks!

typeerror
  • 3 3 Answers
  • 83 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-12T11:14:02+00:00Added an answer on June 12, 2022 at 11:14 am

    The cause: This error happens because you’re attempting to concatenate a list with a string, which is a Python error.

    The solution: I think wwhat you want is to add and remove items from a list, your if/else block should look like this:

    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)
    • 24
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Chloë Moreau
    2022-05-25T20:43:07+00:00Added an answer on May 25, 2022 at 8:43 pm

    This is not how you add an item to your string. This:

    newinv=inventory+str(add)

    This means you are trying to combine a string and a list. Use the list.append() method to add an item or two to a list.

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

    We hope this helps!

    • 21
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. adminxy Expert
    2022-06-25T00:30:32+00:00Added an answer on June 25, 2022 at 12:30 am

    The cause: You get the warning message because you are attempting to concatenate a list with a string, but it is an invalid operation in Python.

    Solution: We advise you to add a new item, and replace the line newinv=inventory+str(add) by this one as below:

    newinv = inventory.append(add)

    Then you need to add or remove items from a list. In this instance, your if/else should be:

    if selection=="use":
     print(inventory)
     remove=input("What do you would like to use? ")
     inventory.remove(remove)
     print(inventory)
    
    elif selection=="pickup":
     print(inventory)
     add=input("What do you would like to pickup? ")
     inventory.append(add)
     print(inventory)

    It is not neccessary to create a new inventory whenever you add an item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.