Python List Indexing and Slicing
Objective 3.1 is the largest part of PCEP's 25% collections block, and half of it is position arithmetic. Indexes run from 0 and from -1 backwards; slices take a start, a stop and a step, exclude the stop, and - unlike indexing - never raise however impossible the numbers are. Get those two rules straight and most of block 3 becomes mechanical.
Data Collections Guide 13 of 26 Beginner
- OSUbuntu 26.04 LTS
- Python3.14.4
- pip25.1.1
- TimeAbout 16 min
Nothing here has changed in Python 3. Slicing and negative indexing work the same way on lists, tuples and strings, which is why this guide is the foundation for guide 15 and guide 17 as well.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Python 3.14.4 - the only machine this path needs | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for the largest part of PCEP's collections block. This matters because indexing raises and slicing never does - so an out-of-range slice hands back an empty list rather than an error.
- indexing from both ends, and out of range in both directions
- slicing, and finding that a slice never raises
- using a slice as a target, for assignment and for deletion
- testing membership with
in - building a list four different ways
Before you start
-
Indexing from both ends
-
Indexing out of range, in both directions
-
Slicing
-
A slice never raises
-
Slices as targets: assignment and deletion
-
Membership: in and not in
-
Building a list
-
What the exam does with this half of the objective