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

PY add.py
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)
Output
['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

PY remove.py
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)
Output
['cat', 'fish', 'dog', 'bird']
bird fish ['cat', 'dog']
['dog']
[]
MethodRemovesReturns
remove(x)The first item equal to x (error if missing)None
pop() / pop(i)The last item / the item at iThe removed item
del lst[i], del lst[a:b]By position or slice— (it's a statement)
clear()EverythingNone

Searching and counting

PY search.py
letters = ["a", "b", "c", "b", "a"]

print(letters.index("b"))
print(letters.index("b", 2))
print(letters.count("a"))
print("z" in letters)
Output
1
3
2
False

index raises a ValueError if the item isn't there, so check with in first when unsure.

Sorting

PY sort.py
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)
Output
[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()

PY sorted.py
original = [3, 1, 2]

result = original.sort()
print(result, original)

data = [3, 1, 2]
new_list = sorted(data)
print(new_list, data)
Output
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

PY sort_dicts.py
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)])
Output
['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

PY reverse_copy.py
nums = [1, 2, 3]
nums.reverse()
print(nums)

backup = nums.copy()
nums.append(0)
print(nums, backup)
Output
[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:

PY stack.py
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("(("))
Output
True False False

Quick reference

MethodWhat 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

Try it yourself

Try it yourself
Console output