Python’s list is a mutable sequence with a small, fixed set of methods. The key
thing to know is which ones mutate in place (and return None) versus which
ones return a value. This is a searchable offline reference.
Complete method table
| Method | Signature | Returns | Mutates? |
|---|---|---|---|
append | append(x) | None | Yes |
extend | extend(iterable) | None | Yes |
insert | insert(i, x) | None | Yes |
remove | remove(x) | None | Yes |
pop | pop(index=-1) | removed element | Yes |
clear | clear() | None | Yes |
sort | sort(*, key=None, reverse=False) | None | Yes |
reverse | reverse() | None | Yes |
index | index(x, start, end) | int position | No |
count | count(x) | int tally | No |
copy | copy() | new list | No |
In-place vs returns-a-value: the core distinction
- In-place, returns
None—append,extend,insert,remove,sort,reverse,clear. These change the list itself; assigning their result (x = x.sort()) is a classic bug that storesNone. - Returns a value —
popreturns the removed element,indexreturns a position,countreturns a tally,copyreturns a new list.
The single most common mistake is items = items.sort(). Use items.sort() to
sort in place, or sorted(items) for a new sorted list.
append vs extend
a = [1, 2]
a.append([3, 4]) # a -> [1, 2, [3, 4]] (one nested element)
b = [1, 2]
b.extend([3, 4]) # b -> [1, 2, 3, 4] (concatenated)
remove vs pop
remove(value) deletes the first occurrence of a value by equality — it raises ValueError if the value is absent. pop(index) deletes and returns the element at a given index, defaulting to the last element.
items = [10, 20, 30, 20]
items.remove(20) # items -> [10, 30, 20] (first 20 removed, no return value)
val = items.pop(0) # val = 10, items -> [30, 20]
Sorting: key and reverse
list.sort() sorts in place using the natural ordering. Use key to sort by a derived value, and reverse=True for descending order:
words = ["banana", "apple", "cherry"]
words.sort(key=len) # -> ['apple', 'banana', 'cherry'] (by length)
words.sort(reverse=True) # -> ['cherry', 'banana', 'apple']
sorted(items) does the same but returns a new list and leaves the original unchanged.
Notes
pop(0)removes the first element but is O(n) — prefercollections.dequefor FIFO queues.copy()is a shallow copy — nested mutable objects are shared between original and copy; usecopy.deepcopyfor a fully independent clone.len,sorted,reversed,min,maxare built-in functions, not list methods.list.sort()is guaranteed stable in Python 3 — equal elements keep their original relative order.