Lists

Introduction

A list holds many values in order, inside square brackets. Unlike a string, a list can be changed after it is created, which makes it powerful and, sometimes, surprising.

Programs rarely handle one value at a time. They deal with all the marks in a class or every item in a cart. A list lets one name stand for many values, and a single loop can handle all of them.

How it works

Making and using a list

marks = [72, 85, 64]
marks.append(90)
print(marks[0], marks[-1])
print(len(marks), sum(marks))
Open in compiler

In the objects panel the list is drawn as a row of boxes with their positions (0, 1, 2, 3), and the variable marks has an arrow pointing to it. The variable does not hold the list itself; it points to it.

  • marks[0] is the first item and marks[-1] the last.
  • append(90) adds an item at the end. Watch the box gain a fourth cell.
  • len() counts the items and sum() adds them up.

Two names, one list

a = [1, 2, 3]
b = a
c = a.copy()
b.append(4)
print(a, c)
Open in compiler

This is the most important picture in this lesson. After b = a, two arrows point to the same list. So b.append(4) changes the list that a points to as well. a.copy() makes a separate list, so c keeps [1, 2, 3].

Syntax

marks = [72, 85, 64]
marks[0]              # first item
marks[-1]             # last item
marks.append(90)
len(marks), sum(marks), max(marks)
copy = marks.copy()

Example

nums = [4, 9, 2, 7]
biggest = nums[0]
for x in nums:
    if x > biggest:
        biggest = x
print(biggest)
Open in compiler

This finds the largest number by hand. biggest starts as the first item; then every item is compared with it, and the calculation box shows each check: 4 > 4, 9 > 4, 2 > 9, 7 > 9. Python's built-in max(nums) does the same job in one call.

Line by line

  1. nums is the list to examine.
  2. biggest = nums[0] takes the first item as the best so far.
  3. for x in nums: visits each item.
  4. If x is larger than the best so far, biggest becomes x.
  5. After the loop, biggest holds the largest item, and it is printed.

Expected result

9

In the real world

A feed of posts, the rows of a table and the results of a search are all lists. The sharing rule causes real bugs: a function that changes a list it was given also changes the caller's list, which is why careful code makes a copy first.

Common mistakes

  • marks[4] on a list of four items is an IndexError; the last position is 3.
  • Expecting b = a to copy a list. It only adds a second name; use a.copy() or list(a).
  • Adding or removing items from a list while looping over it. Loop over a copy instead.

Best practices

  • Keep items of the same kind in one list.
  • Use for x in items unless you need the position.
  • Copy a list before changing it if someone else may still use it.

Summary

A list holds values in order and can change. Positions start at 0. Variables point to lists, so assigning a list makes a second name for it, and copy() makes a separate list.

Practice

Read five marks on one line, store them in a list with split() and int(), and print the average with two decimal places.

Quick check

1. After nums = [5, 6] and nums.append(7), what is len(nums)?
2. After a = [1] ; b = a ; b.append(2), what is a?
3. Which line makes an independent copy of a?

Key takeaways

  • A variable points to a list, and does not hold it.
  • b = a makes a second name, not a copy.
  • Use a.copy() for a separate list.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.