Select from Lists and Text
Select one reading, character, or ordinary range from an ordered sequence while keeping zero-based indexes and slice boundaries explicit.
Chapter 4 passed a complete list into mean(readings). Sometimes a program
needs the whole list. At other times it needs one reading or one meaningful
part of the sequence.
readings = [18, 21, 21, 20, 23]
This list preserves order, including the repeated value 21. Its length is 5:
print(len(readings)) # 5
Select One Item by Its Position
Python numbers sequence positions from zero. The position is called an index:
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Reading | 18 | 21 | 21 | 20 | 23 |
Use square brackets to select one item:
Negative indexes count back from the end. -1 selects the last item, -2
selects the item before it, and so on:
An index must name an existing position. readings[5] fails because the five
items have indexes 0 through 4. Python reports IndexError: list index out of range. Check the length or choose a valid index instead of guessing.
Q1. Select from the end
Which expression selects 20 from readings = [18, 21, 21, 20, 23] by
counting back from the end?
Select one choice, then check.
HintAnchor the count at the end
readings[-1] is 23.
SolutionUse index -2
readings[-2] selects 20, the second item from the end.
Select a Range with a Slice
An index selects one item. A slice selects a range and returns a new sequence of the same kind:
central = readings[1:4] # [21, 21, 20]
Read 1:4 as “start at index 1 and stop before index 4.” Including the start
but excluding the stop makes adjacent ranges meet without sharing an item:
Omitting the start means “from the beginning.” Omitting the stop means “through the end.” These ordinary forms are enough for the selections in this chapter.
The same model works for other ordered sequences. A string is an ordered sequence of characters:
A tuple is a small fixed sequence written with parentheses. Lesson 4 will use tuples to keep related values together; selection follows the same rule:
Q2. Distinguish an item from a range
Given label = "temperature", which expression returns the string "temp"?
Select one choice, then check.
HintThe stop is excluded
The characters at indexes 0, 1, 2, and 3 spell temp.
SolutionTake the first four characters
label[:4] selects from the beginning through index 3 and returns "temp".
Ask Whether a Sequence Contains a Value
Use in when the question is about presence rather than position:
Membership does not reveal where a value occurs or how many times it occurs. It answers one question: is the value present?
Before relying on the first or last item, a program can check whether the sequence contains any items. The explicit length check makes the condition clear:
Python also treats a non-empty sequence as true and an empty sequence as false:
Use the shorter form after the length meaning is understood. Neither form says anything about whether the contained numerical values are zero; it checks only whether the sequence has an item.
Q3. Repair an invalid selection
The program should display the last reading for either list. Replace the fixed index with a selection that remains valid when the list length changes.
Editable Python
Ready to run.
HintCount from the end
Use the negative index that always means “last item.”
SolutionSelect the final item
Indexes select one ordered item, slices select a range, and membership asks whether a value is present. Once selection is predictable, a program can begin with an empty list and build the values it wants to keep.