W3docs

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

MethodWhat it doesMutates list?Returns
append(x)Add one item to the endYesNone
extend(iterable)Add all items from an iterable to the endYesNone
insert(i, x)Insert one item at position iYesNone
remove(x)Remove first occurrence of xYesNone
pop([i])Remove and return item at index i (default: last)YesRemoved item
index(x[, start[, end]])Return index of first occurrence of xNoint
count(x)Count occurrences of xNoint
sort([key][, reverse])Sort items in placeYesNone
reverse()Reverse items in placeYesNone
copy()Return a shallow copyNoNew list
clear()Remove all itemsYesNone

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.

python— editable, runs on the server

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 element

To 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.

python— editable, runs on the server

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.

python— editable, runs on the server
  • insert(0, x) prepends to the front (but is O(n) because all items shift).
  • If i is 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.

python— editable, runs on the server

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 list

pop()

pop(i) removes the item at index i and returns it. The return value is the key difference from remove().

python— editable, runs on the server

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)   # c

clear()

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.

python— editable, runs on the server

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 >= 2

index() 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.

python— editable, runs on the server

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.
python— editable, runs on the server

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.

python— editable, runs on the server

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']  — unchanged

Copying

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.

Practice

Practice
Which Python list method removes every element from the list and returns None?
Which Python list method removes every element from the list and returns None?
Was this page helpful?