PYTHON PROGRAMMING · 14 MIN READ

Lists: ordered, mutable collections

A list keeps an ordered sequence of values that can be changed after creation.

Learning objectives

  • Index and append to a list.
  • Explain why list indexes begin at zero.

One sequence, many values

PYTHON
tasks = ["read", "trace", "practise"]
tasks.append("review")
first = tasks[0]

The first element is at index 0. append changes the existing list by adding an item to the end. A list can hold mixed types, but a consistent element type is often easier to reason about.

Trace and mistakes

After the append, there are four values; first is "read". An index equal to the list length is out of range. Negative indexes count from the end, which is useful only when intentional.

Applications and references

Lists represent tasks, search results, readings, and records gathered during a program’s run.