Python List Methods and Copying
The second half of objective 3.1. The methods are easy to list and easy to get wrong in one specific way: the mutating ones return None, so xs = xs.sort() destroys the list. Copying is the deeper subject - b = a gives two names for one list, a[:] gives a real copy, and neither copies the lists *inside* a list. That last point is where PCAP's most-asked OOP question comes from.
Data Collections Guide 14 of 26 Beginner
- OSUbuntu 26.04 LTS
- Python3.14.4
- pip25.1.1
- TimeAbout 18 min
Nothing here is version-dependent. list.copy() was added in Python 3.3 and is equivalent to xs[:]; both are shown, because a question may use either.
| 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 second half of the list objective. This matters because b = a copies nothing - both names reach the same list, which is where most wrong answers in this section come from.
- using the methods that add and the methods that remove
- learning that
sort()returns None whilesorted()returns a list, the highest-yield fact here - meeting the two exceptions removal and searching raise, and which is which
- finding that
b = acopies nothing, and that a copy does not reach the inner lists - meeting the repetition trap, and what the exam does with all of it
Before you start
- guide 13 - the first half of objective 3.1.
- Nothing else.
-
The methods that add
-
The methods that remove
-
How removal fails
-
sort() returns None, sorted() returns a list
-
What cannot be sorted
-
Searching and counting
-
index() on something absent
-
b = a does not copy anything
-
A copy of a list of lists is not a copy of the inner lists
-
The repetition trap
-
What the exam does with this half of the objective