Python Python Basics (2015) Python Data Types List Creation

How to make lists

Having trouble making list:

I'm using syntax to make list as:

colors = [red, orange , yellow, green , purple]

I've used with and without "" marks, still getting syntax error, I've watched video twice and not sure what I am missing to make list

lists.py
colors = ["red,orange,yellow,green,purple"]


1 Answer

Ryan S
Ryan S
27,258 Points

Hi Sean,

If you want to create a list of strings, you'd need to make sure that each individual item is in quotes. Once you establish that, then each item will be separated by a comma outside of the quotes.

colors = ["red", "orange", "yellow", "green", "purple"]

Your first example (the one without quotes) would work if each item in the list was a variable. But you'd need to assign a value to them first.

red = "red"
orange = "orange"
yellow = "yellow"
green = "green"
purple = "purple"
colors = [red, orange, yellow, green, purple]

Your other example (the entire list of colors inside quotes) is really only creating a list with one item.

colors = ["red,orange,yellow,green,purple"]

# The one item in the list is "red,orange,yellow,green,purple"

Whatever is inside a pair of quotes is considered one item, a single string. So the commas are also included in that string.

Hope this helps.

Hello Ryan,

Thank you very much for that advice. I did try and create the string exactly how you describe is the correct way and am still getting a syntax error message when I do the code challenge. Could this be a "bug" in the course I am taking? Again, I did it exactly the way you described.

Thanks! :)

Ryan S
Ryan S
27,258 Points

Hi Sean,

That is very strange. If I copy and paste this exact line into the challenge it passes for me.

colors = ["red", "orange", "yellow", "green", "purple"]

If that isn't working for you then I'm not sure what is going on. Would you be able to post the code that you are trying?