Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 4 · Lesson 1 · Core

Lists: many values in order

Make a list, read items by position, count them and add to the end.

  • 55 minutes
  • Serves O4
  • Assumes Module 3

Phase 1: Prepare

Step 1 Warm-up recall

From Module 3: where should a running total start?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • create a list and read an item by position (Foundation);
  • use len, in and append (Core);
  • predict an IndexError (Core).

Phase 2: Teach

Step 3 A familiar example

A register lists students in order: first, second, third. A program does the same with a list, and asks for the item at a given position.

Step 4 Clear explanation

A list holds several values under one name, in square brackets. Each place is called an index, and counting starts at 0.

scores = [70, 85, 60]
print(scores[0])
print(scores[-1])
print(len(scores))
scores.append(90)
print(scores)
print(85 in scores)
70
60
3
[70, 85, 60, 90]
True
[0]: 700[1]: 851[2]: 602[3] or [-1]: 903scores[0] is 70; scores[-1] is 90
A list keeps values in order. Each place has a number, and the first place is number 0.
CodeMeaningResult for scores = [70, 85, 60, 90]
len(scores)how many items4
scores[1]item at position 185
scores[-1]last item90
85 in scoresis it in the list?True

A negative index counts from the end, so [-1] is the last item. Asking for a place that does not exist, such as scores[4] in a list of four, stops the program with an IndexError. Python stops here instead of reading memory that is not yours.

Step 5 Worked example (4 marks)

For names = ["Ann", "Bo", "Cy"] find names[1], names[-1] and len(names).

  1. Position 1 is the second item: Bo. (1 mark)
  2. -1 is the last item: Cy. (1 mark)
  3. There are three items: len is 3. (1 mark)
  4. Answer: Bo, Cy and 3. (1 mark)

Go deeper: the professor's notes

A list is in order and found by position from 0. The last position is len minus 1.
Index

From 0.

Length

len(list).

Beyond

IndexError.

Listmany valuesIndexpositionlencountErrorbeyond end
List

Positions

CodeGives
items[0]first
items[-1]last

At work: A length check

A request asks for 1,000 items from a list of 10.

  1. Compare the request with len.
  2. Refuse if too large.

What a careful practitioner decides. Check a requested position against len before using it.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. In [10, 20, 30], the item at position 0 is:

2. In [10, 20, 30], item [-1] is:

3. len([4, 5, 6, 7]) is:

4. For a list of four items, item [4] gives:

Phase 4: Close

Step 7 Recap

  • A list holds values in order; positions start at 0.
  • len counts, in tests, append adds to the end.
  • A position that does not exist gives an IndexError.

Step 8 Exit ticket (ungraded)

For colours = ["red", "green", "blue"], write colours[1], colours[-1] and len(colours).

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Make a list of five items you buy often and print the third one.

In industry: Heartbleed: reading past the end

The OpenSSL heartbeat feature copied back as many bytes as the sender asked for without checking the real length. Attackers could read up to 64 KB of server memory per request. The bug dated from 2012 and was made public in April 2014.

Link to this lesson. In Python a list stops you: asking for a position that does not exist gives an IndexError instead of reading beyond the end.

Source: OWASP, Heartbleed Bug; Invicti, The Heartbleed Bug

Common mistakes

MistakeWhy it fails
Counting from 1The first item is at position 0.
Using len as the last positionThe last position is len minus 1.
Forgetting that append changes the listThe list is changed in place.

Limits. This lesson covers lists of numbers and text; other list tools appear as needed.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · What is a list?
ValuesmanyOrderkeptPositionfrom 0
List

An ordered collection of values in square brackets. Items are found by position, starting at 0. A list holds a week of daily sales or a class register.

Card 2 of 3 · Why does counting start at 0?
First[0]Last[-1]Countlen
Index

The position counts how far an item is from the start, so the first item is 0 steps away. Always use len minus 1 for the last position.

Card 3 of 3 · What does an IndexError mean?
Ask[4]List4 itemsResulterror
IndexError

You asked for a position that does not exist, such as item 4 in a list of 4. Python stops instead of reading beyond the end. Check len first when the position comes from input.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards