Skip to content

Lists

Deprecated Lab — Trinket is shutting down

This lab was written for Trinket.io, which is shutting down in August 2026. The embedded trinket.io links on this page will stop working after that date.

These pages are kept for reference only. The current version of this course now runs every lab as an inline Skulpt editor right in the page — no account or install needed. Start at Chapter 1: Welcome to Python.

Python List

How would we create and access a list of colors in Python? Here is a list of colors:

1
colorList = ['red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', 'brown', 'gray', 'gold']
To access any color you can reference the index of the color by using a number. For example to get the third color you would use:

1
colorList[2]

Sample Code to Draw A Circle with a Color Index

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import turtle
import random
dan = turtle.Turtle()
dan.shape('turtle')

colorList = ['red', 'orange', 'yellow', 'green', 'blue',
  'purple', 'pink', 'brown', 'gray', 'gold']

dan.begin_fill()
dan.color(colorList[3])
dan.circle(20)
dan.end_fill()

Iterating over many colors

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

color_list = ['red', 'orange', 'yellow', 'green', 'blue',
  'purple', 'pink', 'brown', 'gray', 'gold', 'cyan', 'Gainsboro', 'gray',
  'dimgray', 'LightSlateGray','AliceBlue', 'LimeGreen', 'DarkKhaki', 'Khaki']

dan.penup()
dan.goto(-180, 160)
dan.begin_fill()
for myColor in color_list:
  dan.color(myColor)
  dan.pendown()
  dan.begin_fill()
  dan.circle(10)
  dan.end_fill()
  dan.penup()
  dan.forward(20)
dan.hideturtle()

Drawing

Run Sample Program on Trinket

Draw a Green Circle Using List

Experiments

  1. Can you change the name of the function to be "petal"?
  2. What does print(len(colorList)) return?
  3. Go to the Trinket colors page and see the name of other colors you can use.
  4. What happens if a list does not fit on a single line? Can a list span multiple lines?
  5. Can you use double quotes and single quotes in the same list?