Python · Stage 5 – Grouping Data · Lesson 28 of 50

Lists: Grouping Values

Store an ordered group of values and read items by position.

colors = ["red", "blue", "green"]
Line-by-line explanation
  1. Square brackets begin and end the list.
  2. Commas separate its three string items.
  3. colors stores the entire list.
first = colors[0]
Line-by-line explanation
  1. colors is the list.
  2. [0] asks for its first item.
  3. first stores red.
some = colors[0:2]
Line-by-line explanation
  1. The slice starts at index 0.
  2. It stops before index 2.
  3. some stores red and blue.

Tiny Practice

Display the first fruit.

Starter template

fruits = ["apple", "pear"]
print(CHANGE_ME)
Line-by-line explanation
  1. The list has two items.
  2. The first item has index 0.
Hint
  1. Use the list name.
  2. Add square brackets.
  3. Put 0 inside them.
Show Solution
fruits = ["apple", "pear"]
print(fruits[0])
Line-by-line explanation
  1. The list is stored.
  2. fruits[0] reads apple.
  3. Print displays apple.