1.2.22 · D3Introduction to Programming (Python)

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

2,787 words13 min readBack to topic

The scenario matrix

Before working examples, let us list every case class a question about list methods can hit. Each row is one "kind of situation." Our examples below will each be tagged with the cell(s) it covers, so by the end every cell is filled.

# Case class What is unusual about it Covered by
C1 Normal add (append/insert in the middle) shifting of later items Ex 1
C2 Insert at edges (front i=0, far past end) clamping when i > len Ex 2
C3 Remove by value vs pop by index one returns None, one returns the item Ex 3
C4 Negative index in pop counting from the right Ex 4
C5 Empty / degenerate list pop() on [], index on [] Ex 5
C6 Missing value (remove/index of absent) raises ValueError Ex 6
C7 Out-of-range index (pop(9)) raises IndexError Ex 6
C8 Ties & stability in sort equal keys keep original order Ex 7
C9 Descending / reverse interplay sort then reverse vs reverse=True Ex 8
C10 Query methods (count, index with duplicates) first-occurrence rule Ex 9
C11 Real-world word problem translating English into method calls Ex 10
C12 Return-value trap (x = lst.sort()) None leaks into a variable Ex 10

The figure below is the mental model we will reuse: a row of numbered lockers (indices) with items inside. Every method is a physical action on this row.

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

The worked examples

Example 1 — Normal add with shifting (C1)


Example 2 — Inserting at the edges & clamping (C2)


Example 3 — remove (by value) vs pop (by index) (C3)


Example 4 — Negative index in pop (C4)


Example 5 — Empty / degenerate list (C5)


Example 6 — Missing value & out-of-range index (C6, C7)


Example 7 — Ties & stability in sort (C8)


Example 8 — Descending: sort+reverse vs reverse=True (C9)


Example 9 — Query methods with duplicates (C10)


Example 10 — Word problem + the return-value trap (C11, C12)


Recall Quick self-test (reveal answers)

insert(100, x) on a length-4 list — crash or clamp? ::: Clamp — x goes to the end, no error. pop(100) on a length-4 list — crash or clamp? ::: Crash — IndexError. remove(9) when 9 is absent raises which error? ::: ValueError. pop() on [] raises which error? ::: IndexError (pop from empty list). Is Python's sort stable? ::: Yes — equal keys keep their original relative order. x = lst.sort() sets x to what? ::: None.


Connections

  • List methods — append, insert, remove, pop, sort, reverse, count, index
  • Lists — creation and indexing
  • Mutability vs Immutability in Python
  • sorted() vs list.sort()
  • Tuples — why no append/sort
  • Time complexity — append O(1) vs insert O(n)
  • Strings — index and count methods
  • Exceptions — ValueError and IndexError

Scenario Map

adds

removes

reorders

queries

insert i too big

remove missing value

pop bad index

index missing value

equal keys

sort returns

A list method call

append or insert

remove or pop

sort or reverse

count or index

clamps to end no error

ValueError

IndexError

stable keeps order

None mutates in place