List Methods
Master all 11 Python list methods: append, extend, insert, remove, pop, index, count, sort, reverse, copy, and clear — with runnable examples and gotchas.
Python lists are mutable, ordered sequences. Because they are mutable, the list type ships eleven built-in methods that let you add, remove, search, and reorder items in place — no imports required. This page covers every method with runnable examples, common pitfalls, and guidance on when to choose one over another.
Related chapters: Python Lists · Sort Lists · List Comprehension · Copy Lists
Quick reference
| Method | What it does | Mutates list? | Returns |
|---|---|---|---|
append(x) | Add one item to the end | Yes | None |
extend(iterable) | Add all items from an iterable to the end | Yes | None |
insert(i, x) | Insert one item at position i | Yes | None |
remove(x) | Remove first occurrence of x | Yes | None |
pop([i]) | Remove and return item at index i (default: last) | Yes | Removed item |
index(x[, start[, end]]) | Return index of first occurrence of x | No | int |
count(x) | Count occurrences of x | No | int |
sort([key][, reverse]) | Sort items in place | Yes | None |
reverse() | Reverse items in place | Yes | None |
copy() | Return a shallow copy | No | New list |
clear() | Remove all items | Yes | None |
Adding items
append()
append(x) adds a single item x to the end of the list. It is an O(1) amortized operation and is the idiomatic way to build a list one item at a time.
append() always adds the argument as one element, even if that argument is itself a list:
fruits = ["banana", "orange"]
fruits.append(["apple", "mango"])
print(fruits) # ['banana', 'orange', ['apple', 'mango']]
print(len(fruits)) # 3 — the nested list counts as one elementTo merge all items from another list instead, use extend().
extend()
extend(iterable) appends every item from the given iterable to the end of the list. The iterable can be a list, tuple, set, string, or any other iterable.
extend() is equivalent to fruits += more_fruits. Use it when you already have a collection of items to merge; use append() for a single item.
insert()
insert(i, x) inserts item x before position i. All existing items at index i and beyond shift one position to the right.
insert(0, x)prepends to the front (but is O(n) because all items shift).- If
iis greater than or equal to the list length, the item is appended to the end — no error is raised.
Removing items
remove()
remove(x) scans the list from left to right and deletes the first element equal to x. If x is not found, it raises a ValueError.
Guarding against ValueError:
fruits = ["banana", "orange"]
item = "mango"
if item in fruits:
fruits.remove(item)
else:
print(f"{item} not in list")
# mango not in listpop()
pop(i) removes the item at index i and returns it. The return value is the key difference from remove().
pop() called with no argument removes and returns the last item — a common stack pattern:
stack = ["a", "b", "c"]
last = stack.pop()
print(stack) # ['a', 'b']
print(last) # cclear()
clear() removes every element from the list, leaving it empty. It is equivalent to del lst[:] but more readable.
fruits = ["banana", "apple", "orange"]
fruits.clear()
print(fruits) # []Use clear() when you want to empty a list that other variables might also reference — fruits = [] would just rebind the name, while clear() empties the underlying object.
Searching items
index()
index(x) returns the integer index of the first occurrence of x. It accepts optional start and end arguments to limit the search to a slice.
Searching within a range:
items = ["a", "b", "c", "b", "d"]
print(items.index("b")) # 1 — first occurrence
print(items.index("b", 2)) # 3 — first occurrence at index >= 2index() raises ValueError if the item is absent. Use in to check first when the absence is possible:
if "mango" in fruits:
pos = fruits.index("mango")count()
count(x) returns the number of times x appears in the list.
Unlike index(), count() never raises an error for a missing value — it returns 0.
Ordering items
sort()
sort() sorts the list in place in ascending order by default. It accepts two optional keyword arguments:
reverse=True— sort in descending order.key=<callable>— a function applied to each element before comparing.
Sorting in descending order:
numbers = [3, 1, 4, 1, 5]
numbers.sort(reverse=True)
print(numbers) # [5, 4, 3, 1, 1]Sorting strings case-insensitively with key:
words = ["banana", "Apple", "cherry", "date"]
words.sort(key=str.lower)
print(words) # ['Apple', 'banana', 'cherry', 'date']sort() mutates the list and returns None. If you need a sorted copy without changing the original, use the built-in sorted() function instead:
original = [3, 1, 4]
result = sorted(original)
print(original) # [3, 1, 4] — unchanged
print(result) # [1, 3, 4]See Sort Lists for an in-depth guide including custom comparators and sorting objects.
reverse()
reverse() reverses the list in place. Like sort(), it returns None.
To get a reversed copy without mutating the original, use reversed() (returns an iterator) or slicing:
fruits = ["banana", "apple", "orange"]
print(list(reversed(fruits))) # ['orange', 'apple', 'banana']
print(fruits[::-1]) # ['orange', 'apple', 'banana']
print(fruits) # ['banana', 'apple', 'orange'] — unchangedCopying
copy()
copy() returns a shallow copy of the list — a new list object containing the same item references.
original = ["banana", "apple", "orange"]
clone = original.copy()
clone.append("mango")
print(original) # ['banana', 'apple', 'orange'] — unchanged
print(clone) # ['banana', 'apple', 'orange', 'mango']"Shallow" means that nested objects (like a list inside a list) are not duplicated — both the original and the copy will reflect changes to those nested objects. For a fully independent duplicate, use copy.deepcopy() from the standard library. See Copy Lists for a detailed explanation of shallow vs. deep copies.
Common pitfalls
All mutating methods return None. A common mistake is assigning the result of sort(), reverse(), append(), etc.:
# Wrong — result is None, not a sorted list
numbers = [3, 1, 2]
numbers = numbers.sort()
print(numbers) # None# Correct — sort() mutates in place
numbers = [3, 1, 2]
numbers.sort()
print(numbers) # [1, 2, 3]remove() and index() raise ValueError for missing items. Always check with in first, or catch the exception, when the item might not be present.
append() vs extend() for lists. Using append(another_list) nests the list as a single element; extend(another_list) merges its items.