Python Python Basics (2015) Python Data Types List Creation

Not sure if I am creating this list correctly?

So this prompt asks me to create a new variable called colors with a list of 5 colors. The tutorial video beforehand only shows lists made with numbers.

Going off that, I first did the following:

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

Then, I got the error message saying that 'red' was not defined.

Following that, I attached the following code:

list = [red, yellow, green, blue, purple] colors = list

Does anyone know how to solve this so that I can create a list with colors listed in words? I understand how to make lists with numbers, but not with words.

lists.py
list = [red, yellow, green, blue, purple]
colors = list 

2 Answers

Alex Arzamendi
Alex Arzamendi
10,042 Points

The thing is, when you pass the code in such way, the python interpreter thinks that the colors defined inside the list are variable holders, meaning that it expects red, yellow etc to have values inside of them, if you want to pass them as strings then you would need to change your code to:

# Create a list with string values representing colors
list = ["red", "yellow", "green", "blue", "purple"]

# copies the stored list into a colors variable which will also hold the same information
colors = list

This is not only for strings, you could have defined red etc before and given them values and then you would be able to pass it to list without it telling you that they have not been defined. Try it!

Thanks, Alex! That works. I'm sometimes still not sure when and when not to use quotes in Python.

You need to create a list of strings and not a list of variables. What you are doing right now is telling it to create a list of the variable red, the variable yellow and so on. By putting quotes around them they become strings and it doesn't try to find them.

The other thing is that you don't need to assign the list to list and then reassign it to colors. You can directly assign it to colors.

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

Happy coding!