Search code examples
pythontypeerror

How to fix TypeError: can only concatenate str (not "list") to str


I am trying to learn python from the python crash course but this one task has stumped me and I can’t find an answer to it anywhere

The task is Think of your favourite mode of transportation and make a list that stores several examples Use your list to print a series of statements about these items

cars = ['rav4'], ['td5'], ['yaris'], ['land rover tdi'] 

print("I like the "+cars[0]+" ...")

I’m assuming that this is because I have letters and numbers together, but I don’t know how to produce a result without an error and help would be gratefully received The error I get is

TypeError: can only concatenate str (not "list") to str**


Solution

  • Your first line actually produces a tuple of lists, hence cars[0] is a list.

    If you print cars you'll see that it looks like this:

    (['rav4'], ['td5'], ['yaris'], ['land rover tdi'])
    

    Get rid of all the square brackets in between and you'll have a single list that you can index into.