List Methods
Lists have a compact set of methods for adding, removing, searching and ordering items. Most of them change the list in place and return None — an important detail that catches many beginners.
Adding items
items = ["a", "b"] items.append("c") print(items) items.insert(1, "x") print(items) items.extend(["d", "e"]) print(items) items.append(["f", "g"]) print(items)
['a', 'b', 'c'] ['a', 'x', 'b', 'c'] ['a', 'x', 'b', 'c', 'd', 'e'] ['a', 'x', 'b', 'c', 'd', 'e', ['f', 'g']]
append adds one item (even if that item is a list); extend adds each item of an iterable; insert(i, x) puts x before position i.
Removing items
pets = ["cat", "dog", "fish", "dog", "bird"] pets.remove("dog") print(pets) last = pets.pop() second = pets.pop(1) print(last, second, pets) del pets[0] print(pets) pets.clear() print(pets)
['cat', 'fish', 'dog', 'bird'] bird fish ['cat', 'dog'] ['dog'] []
| Method | Removes | Returns |
|---|---|---|
remove(x) | The first item equal to x (error if missing) | None |
pop() / pop(i) | The last item / the item at i | The removed item |
del lst[i], del lst[a:b] | By position or slice | — (it's a statement) |
clear() | Everything | None |
Searching and counting
letters = ["a", "b", "c", "b", "a"] print(letters.index("b")) print(letters.index("b", 2)) print(letters.count("a")) print("z" in letters)
1 3 2 False
index raises a ValueError if the item isn't there, so check with in first when unsure.
Sorting
nums = [5, 2, 9, 1] nums.sort() print(nums) nums.sort(reverse=True) print(nums) words = ["banana", "Apple", "cherry", "fig"] words.sort(key=str.lower) print(words) words.sort(key=len) print(words)
[1, 2, 5, 9] [9, 5, 2, 1] ['Apple', 'banana', 'cherry', 'fig'] ['fig', 'Apple', 'banana', 'cherry']
The key argument is a function applied to each item to decide its sort position — str.lower for case-insensitive order, len for length. Python's sort is stable: items with equal keys keep their original relative order, which is why "banana" stays before "cherry".
sort() vs sorted()
original = [3, 1, 2] result = original.sort() print(result, original) data = [3, 1, 2] new_list = sorted(data) print(new_list, data)
None [1, 2, 3] [1, 2, 3] [3, 1, 2]
list.sort() sorts in place and returns None — so x = x.sort() is a bug that throws your list away. sorted() returns a new sorted list and works on any iterable.
Sorting complex data
from operator import itemgetter people = [ {"name": "Ana", "age": 31}, {"name": "Ben", "age": 25}, {"name": "Cleo", "age": 31}, ] by_age = sorted(people, key=lambda p: p["age"]) print([p["name"] for p in by_age]) by_age_desc_then_name = sorted(people, key=lambda p: (-p["age"], p["name"])) print([p["name"] for p in by_age_desc_then_name]) print([p["name"] for p in sorted(people, key=itemgetter("name"), reverse=True)])
['Ben', 'Ana', 'Cleo'] ['Ana', 'Cleo', 'Ben'] ['Cleo', 'Ben', 'Ana']
Returning a tuple from the key function sorts by several fields at once; negating a number reverses just that field.
Reversing and copying
nums = [1, 2, 3] nums.reverse() print(nums) backup = nums.copy() nums.append(0) print(nums, backup)
[3, 2, 1] [3, 2, 1, 0] [3, 2, 1]
Lists as stacks
append and pop together make a list a fast stack (last in, first out) — useful for undo history, or tracking nested brackets:
def balanced(text): pairs = {")": "(", "]": "[", "}": "{"} stack = [] for ch in text: if ch in "([{": stack.append(ch) elif ch in pairs: if not stack or stack.pop() != pairs[ch]: return False return not stack print(balanced("(a[b]{c})"), balanced("(]"), balanced("(("))
True False False
Quick reference
| Method | What it does |
|---|---|
append(x), extend(it), insert(i, x) | Add items |
remove(x), pop([i]), clear() | Remove items |
index(x), count(x) | Search |
sort(key=, reverse=), reverse() | Reorder in place |
copy() | Shallow copy |