PythonList Methods

List Methods

A Python list comes with a full set of built-in methods for adding, removing, searching, and reordering its items. This page walks through every one of them, with an emphasis on the two that trip people up the most: sort() and the difference between modifying a list in place versus getting back a new one.

Complete Method Reference

Method

Signature

What it does

Returns

append

append(x)

Adds x as a single new item at the end of the list.

None

extend

extend(iterable)

Appends every item from iterable to the end, one by one.

None

insert

insert(i, x)

Inserts x at index i, shifting existing items to the right.

None

remove

remove(x)

Removes the first item equal to x. Raises ValueError if not found.

None

pop

pop([i])

Removes and returns the item at index i (default: the last item).

The removed item

clear

clear()

Removes every item, leaving the list empty.

None

index

index(x[, start[, end]])

Returns the index of the first item equal to x. Raises ValueError if absent.

int

count

count(x)

Counts how many times x appears in the list.

int

sort

sort(key=None, reverse=False)

Sorts the list in place using < comparisons (or key, if given).

None

reverse

reverse()

Reverses the order of items in place.

None

copy

copy()

Returns a shallow copy of the list — equivalent to list_a[:].

A new list

append, extend, and insert

Python
nums = [1, 2, 3]

nums.append(4)
print(nums)  # [1, 2, 3, 4]

nums.extend([5, 6])
print(nums)  # [1, 2, 3, 4, 5, 6]

nums.insert(0, 0)
print(nums)  # [0, 1, 2, 3, 4, 5, 6]
Note
`append(x)` always adds exactly one item, even if `x` is itself a list. `extend(iterable)` unpacks its argument and adds each element separately. `nums.append([5, 6])` produces `[1, 2, 3, [5, 6]]`, not `[1, 2, 3, 5, 6]`.
remove, pop, and clear

Python
letters = ["a", "b", "c", "b"]

letters.remove("b")     # removes the FIRST "b" only
print(letters)           # ['a', 'c', 'b']

last = letters.pop()    # removes and returns the last item
print(last, letters)     # b ['a', 'c']

first = letters.pop(0)  # removes and returns index 0
print(first, letters)    # a ['c']

letters.clear()
print(letters)           # []
index and count

Python
nums = [10, 20, 30, 20, 40]

print(nums.index(20))       # 1  - first match
print(nums.index(20, 2))    # 3  - search starting from index 2
print(nums.count(20))       # 2  - appears twice

# print(nums.index(99))     # ValueError: 99 is not in list
sort() and reverse()

sort() reorders the list in place using each item’s natural ordering by default. Pass reverse=True to sort descending, or a key function to control exactly what value each item is compared by.

Python
nums = [4, 1, 3, 2]
nums.sort()
print(nums)  # [1, 2, 3, 4]

nums.sort(reverse=True)
print(nums)  # [4, 3, 2, 1]

words = ["banana", "kiwi", "fig", "watermelon"]
words.sort(key=len)          # sort by string length
print(words)                  # ['fig', 'kiwi', 'banana', 'watermelon']

words.sort(key=lambda w: len(w), reverse=True)
print(words)                  # ['watermelon', 'banana', 'kiwi', 'fig']

The key parameter really shines when sorting more complex items, like a list of dictionaries, where you need to pick out one field to compare by.

Python
people = [
    {"name": "Ada", "age": 36},
    {"name": "Grace", "age": 85},
    {"name": "Alan", "age": 41},
]

people.sort(key=lambda person: person["age"])
for p in people:
    print(p["name"], p["age"])
# Ada 36
# Alan 41
# Grace 85

# Sort by age, oldest first
people.sort(key=lambda person: person["age"], reverse=True)
print(people[0]["name"])  # Grace

Python
numbers = [3, 1, 2]
numbers.reverse()
print(numbers)  # [2, 1, 3]  - just reverses order, does NOT sort
sort() mutates in place — sorted() does not
`list.sort()` reorders the existing list and returns `None`. A very common bug is writing `numbers = numbers.sort()`, which throws away the list and leaves `numbers` set to `None`. If you want a new, sorted list while keeping the original untouched, use the built-in `sorted()` function instead, which returns a fresh list and leaves its input unchanged.

Python
original = [3, 1, 2]

# WRONG - sort() returns None
# original = original.sort()
# print(original)  # None

# RIGHT - sort() mutates in place
original.sort()
print(original)  # [1, 2, 3]

# RIGHT - sorted() returns a new list, original untouched
nums = [3, 1, 2]
result = sorted(nums)
print(nums)     # [3, 1, 2]  - unchanged
print(result)   # [1, 2, 3]  - new sorted list
copy()

copy() returns a shallow copy: a new list object containing references to the same items. Modifying the top-level list (adding or removing items) does not affect the original, but mutating a shared inner object still shows up in both.

Python
original = [1, 2, 3]
duplicate = original.copy()   # same as original[:]

duplicate.append(4)
print(original)   # [1, 2, 3]     - untouched
print(duplicate)  # [1, 2, 3, 4]

nested = [[1, 2], [3, 4]]
shallow = nested.copy()
shallow[0].append(99)
print(nested)   # [[1, 2, 99], [3, 4]]  - inner list is shared!
Tip
For a true deep copy that also duplicates nested lists and other mutable objects inside, use `copy.deepcopy()` from the standard library’s `copy` module.