Skip to content

Color Lists

By the end of this lesson you'll be able to:

  • Create a list of color strings and access items by index number
  • Loop through every item in a list using for color in my_list:
  • Draw a row of colored shapes by iterating over a color list

A list is an ordered collection of values. In Python you write it with square brackets. Lists are perfect for storing a palette of colors and then painting shapes one by one through the whole palette.

Welcome to This Lesson!

Monty waving welcome What if you could tell Monty: "here are ten colors — paint one circle for each"? That's exactly what lists let you do! Let's code it together!

Creating and Using a List

A list stores multiple values in one variable, separated by commas inside square brackets:

1
colors = ['red', 'orange', 'yellow', 'green', 'blue']

Access any item by its index — its position in the list, counting from 0:

1
2
3
colors[0]   # 'red'
colors[2]   # 'yellow'
colors[4]   # 'blue'

To do something with every item, use a for loop directly over the list:

1
2
for color in colors:
    # 'color' is each item in turn

Scratch Bridge

Monty with a tip In Scratch you used the List block with numbered slots (item 1, item 2, …). In Python, lists use the same idea — but counting starts at 0, not 1. So colors[0] is what Scratch calls "item 1 of colors".

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import turtle
monty = turtle.Turtle()
monty.shape('turtle')
monty.speed(10)

colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink']

monty.penup()
monty.goto(-150, 0)

for color in colors:
    monty.color(color)
    monty.pendown()
    monty.begin_fill()
    monty.circle(20)
    monty.end_fill()
    monty.penup()
    monty.forward(45)

monty.hideturtle()

What Do You Think Will Happen?

Monty thinking The list has 7 colors and the loop runs once per color. How many circles will appear, and what order will the colors go in? Make your guess — then click Run!

Try It Now

Edit the code below and click Run to see the result right on this page. No account needed — everything runs in your browser.


  

Were you right? Seven filled circles spread in a row, each painted a different color from the list — a rainbow palette!

How It Works

for color in colors: is a for-each loop — on each repetition, color is automatically set to the next item in the list. The first time through it's 'red', then 'orange', and so on until the list is exhausted.

forward(45) moves the turtle 45 steps to the right after each circle so the next one appears beside it without overlapping.

Explanation Table

Line What it does
colors = ['red', …] Creates a list of color strings
colors[0] Accesses the first item ('red'); indexes start at 0
for color in colors: Loops once for each item; color holds the current item
monty.goto(-150, 0) Moves to the left side of the canvas as the starting point
monty.forward(45) Shifts right 45 steps before drawing the next circle

Index Out of Range

Monty warning If your list has 7 items, valid indexes are 0–6. Trying colors[7] causes an IndexError — Python can't find item 7 because it doesn't exist. Use len(colors) to find out how many items are in a list.

Learning Check

Your Turn — Draw Squares Instead

Monty thinking The program below is supposed to draw a row of small filled squares (not circles), but the loop body is incomplete. Add two lines inside the loop — a for loop that draws four sides — so each color becomes a filled square instead of a circle.


  

Experiments

Try these changes to the color-circles code. Predict first, then run!

  1. Add 'gold' and 'brown' to the end of the colors list. You'll know it worked when two extra circles appear to the right of the original seven.

  2. Change monty.circle(20) to monty.circle(15) and monty.forward(45) to monty.forward(35). You'll know it worked when the circles are smaller and tighter together.

  3. Print the length of the list: add print(len(colors)) after the list definition and before the loop. You'll know it worked when 7 (or your new count) appears in the output box below the editor.

  4. Access a single color by index: add print(colors[3]) after the list definition. You'll know it worked when green appears in the output (it's at index 3).

  5. Try a for loop using an index: replace for color in colors: with for i in range(len(colors)): and then use colors[i] instead of color. You'll know it worked when the output looks the same — this is an alternative way to iterate.

Great Work!

Monty celebrating Lists let you store whole collections of data and loop through them automatically! In the next lab you'll combine lists with random numbers and functions to draw random stars. Let's code it together!