1.2.22 · D1Introduction to Programming (Python)

Foundations — List methods — append, insert, remove, pop, sort, reverse, count, index

2,578 words12 min readBack to topic

Before you can trust the eight actions on the parent page, you need eight tiny ideas locked in. This page builds each one from zero, in an order where every idea leans on the one before it. Nothing here assumes you have seen Python code before line one — the names of the actions (append, pop, and the rest) are introduced only after the shelf itself is clear.


1. A "value" — the thing in the box

Picture: think of a value as a coloured ball. 3 is one ball, 'a' is another ball. On its own it just sits there.

Why the topic needs it: every action on the shelf takes or returns values — later you will count how many balls equal x, or take out a ball and hold it. If "value" is fuzzy, all of that stays fuzzy.

The parent uses quotes ' ' around letters ('a', 'b'). Those quotes are the signal "this is a string value, treat these characters as text, not as code."


2. The list — a row of boxes

Picture: a row of boxes on a shelf, left to right. Each box holds exactly one value.

Figure — List methods — append, insert, remove, pop, sort, reverse, count, index

Look at the figure: the balls sit inside boxes, and the boxes sit in a fixed left-to-right row. That row-with-fixed-order is the whole reason "position" will matter in the next idea.

Why the topic needs it: the list is the shelf that every action works on. See Lists — creation and indexing for how you build one from scratch.


3. The index — the number under each box

This is the single most important symbol on the parent page, so we go slow.

Picture: below each box, paint a small number. The leftmost box gets 0.

Figure — List methods — append, insert, remove, pop, sort, reverse, count, index

Why the topic needs it:

  • insert(i, x) uses i as an index — where to squeeze the new value.
  • pop(i) uses i as an index — which box to empty.
  • index(x) returns an index — which box the value lives in.

If you mix up "index" (a position number) with "value" (the content), you get the parent's classic trap: lst.remove(0) looks like "remove box 0" but actually means "remove the value 0." That whole bug is a value-vs-index confusion.

Counting from the back: negative indices

Picture: paint a second number under each box, this time starting at the far-right box with -1 and growing more negative as you walk left.

Figure — List methods — append, insert, remove, pop, sort, reverse, count, index

Why the topic needs it: you will meet lst[-1] and pop(-1) the moment you touch real code, so the mental model must include the back-counting numbers, not just the front-counting ones.


4. Length n — how many boxes

Picture: count the boxes in the row. If there are 3 boxes, .

Here is the key link between length and index, and it is worth staring at:

Why and not ? Because index 0 "used up" one of the counts at the front. With boxes and the first labelled 0, the labels are through — that's exactly different numbers. Box number does not exist; that slot is the next free space, which is precisely why the parent writes:

append drops the value into slot — the first empty spot past the last box.


5. Mutating vs building new — "change in place"

Picture: in place = you walk to the existing shelf and rearrange the actual boxes. Not in place = you buy a brand-new shelf, copy things over, and leave the old one alone.

Figure — List methods — append, insert, remove, pop, sort, reverse, count, index

The left half of the figure (teal) shows one shelf being edited — that's sort(), reverse(), append(). The right half (plum) shows a second shelf being created — that's what sorted(lst) does. Same items, but a different shelf.

Why the topic needs it: this single distinction explains the parent's #1 bug (x = lst.sort() gives None). Methods that edit in place have nothing new to hand back, so they hand back the placeholder None. Deep dive: Mutability vs Immutability in Python and sorted() vs list.sort().


6. "Return" and the value None

Picture: a librarian. Ask her to alphabetise the shelf (sort) — she does it and turns to you empty-handed (None). Ask her to take out the last book (pop) — she does it and places the book in your hands (the returned value).

This "hand-back-or-not" is the entire punchline of the parent's Feynman locker story:

Method Hands something back?
append, insert, remove, sort, reverse No → None
pop, count, index Yes → the item / a number

Why the topic needs it: if you assign the result of a None-returning method to a variable, your data looks lost. Knowing which column a method sits in prevents the trap before you type it.


7. Shifting — why front-edits are "slow"

Picture: people seated in a row of chairs. To seat someone in the middle, everyone to the right stands and shuffles one chair down. That shuffling is the shift.

Before the formulas, one symbol to name: let stand for the current index of some box we are watching — just "the number under whichever box we point at." With that agreed, the parent's rules read:

Every box from position rightward must move right by one to open slot .

Every box to the right of the removed one moves left by one to fill the hole.

remove(x) shifts too. remove first finds the index of the first box holding value x, then deletes that box — and from that moment it behaves exactly like pop at that found index: every box to its right slides left by one to close the gap. The only difference is remove searches by value while pop is told the index outright.

Why the topic needs it: append needs zero shifts (it uses the empty end slot) — that's fast. insert(0, x) shifts everyone — that's slow. This is the seed of Time complexity — append O(1) vs insert O(n).


8. Errors — when a method can't do the job

Picture: you tell the librarian "throw away the red book" but there is no red book — she throws up her hands and refuses. That refusal is the error; which refusal (missing value vs. out-of-range slot vs. can't-compare) is which error.

Why the topic needs it: the parent's traps all end in a raised error. lst.remove(0) on [5,6,7]ValueError (no value 0). [1, 'a'].sort()TypeError (a number and a string can't be ordered against each other, because < isn't defined between them). Knowing which error and why lets you read the message instead of panicking. More at Exceptions — ValueError and IndexError.


How these feed the topic

The diagram below is a prerequisite map: each box is one foundation from this page, and an arrow means "the idea at the tail is needed to understand the idea at the head." Read arrows as "leads into." The final box on the right, "Eight list methods," is the parent topic that all these foundations point toward.

Value - a single ball

List - row of boxes

Index - box number from 0

Negative index - count from the back

Length n = len(lst)

Mutable - change in place

Return and None

Shifting to open or close a gap

ValueError IndexError TypeError

Eight list methods

Read it top-down: value → list → index → length is the backbone. Index → negative index → return/None explains what pop() hands back and why pop() defaults to the last box. Mutable → return/None explains what methods hand back. Index + length → shifting explains speed. Value + index → errors explains failures. All arrows land on the eight methods.


Equipment checklist

Cover the right side and answer aloud. If any answer is shaky, re-read that section before the parent page.

What is the difference between a value and an index?
A value is the content in a box (like 3 or 'a'); an index is the box's position number.
In [3, 1, 2], what index holds the value 2?
Index 2 (counting starts at 0).
What does index -1 refer to?
The last box in the list; -2 is second-to-last, and so on (counting from the back).
If a list has length , what is its last valid index?
.
Why does append place the item at index n?
n is the first empty slot just past the last box (indices 0 to n-1 are full).
What does "in place" (mutable) mean?
The same list is edited directly, no new list is created.
What value do sort, reverse, and append return, and why?
None — they only rearrange in place, so there's nothing new to hand back.
Which methods actually hand a useful value back?
pop (the removed item), count (a number), index (a position).
What does pop() with no argument do, and why?
Removes and returns the last item, because its default index is -1.
In the shift rules, what does the symbol mean?
The current index of whichever box we are watching.
Why is insert(0, x) slower than append(x)?
insert(0, x) shifts every existing box right by one; append shifts nothing.
When remove(x) deletes a value, what happens to boxes on its right?
They each shift left by one to close the gap (just like pop at that index).
What error does remove(x) raise when x is absent?
ValueError.
What error does [1, 'a'].sort() raise, and why?
TypeError — a number and a string can't be compared with <.