Python list methods add items (append, insert, extend), remove items (remove, pop, clear), sort items (sort, reverse), and search them (index, count). Most of them change the list in place and return None. Knowing this prevents the most common list mistake.
The examples use the daily order list at Corner Café, a small café in the fictional port city Port Alder.
Which list method does what?
| Method | What it does | Returns |
|---|---|---|
append(x) | Adds one item at the end | None |
insert(i, x) | Adds an item at position i | None |
extend(items) | Adds every item of another list | None |
remove(x) | Removes the first item equal to x | None |
pop(i) | Removes the item at position i (last by default) | The removed item |
clear() | Empties the list | None |
sort() | Sorts the list in place | None |
reverse() | Reverses the current order | None |
copy() | Makes a separate copy | A new list |
index(x) | Finds the first position of x | An integer |
count(x) | Counts how often x appears | An integer |
What is the difference between append and extend in Python?
append adds one item. extend adds each item of another list.
orders = ["tea", "toastie"] orders.append("latte") print(orders) orders.insert(0, "bagel") print(orders) orders.extend(["lemonade", "tea"]) print(orders)
append(x)puts one item at the end.insert(i, x)puts the item at positioniand moves the rest.extend(items)adds the items of another list one by one.
If you used append(["lemonade", "tea"]), the two drinks would not be added separately. One item that is itself a list would go at the end: [..., 'latte', ['lemonade', 'tea']].
How do you remove an item from a list in Python?
Use remove when you know the value. Use pop when you know the position or need the removed item.
orders = ["bagel", "tea", "toastie", "latte", "tea"] orders.remove("tea") print(orders) last = orders.pop() print(last, orders) first = orders.pop(0) print(first, orders)
remove(x)removes the first item equal tox. If it is not in the list, Python raises aValueError.pop()removes and returns the last item.pop(i)does the same for positioni. On an empty list it raises anIndexError.clear()empties the whole list.
pop is the right choice when you need the removed item, for example to take the next order and prepare it.
How do you sort a list in Python?
Call sort() to sort the list in place. Add reverse=True for descending order.
prices = [4.25, 1.25, 3.75, 2.00] prices.sort() print(prices) prices.sort(reverse=True) print(prices)
To keep the original list and get a sorted copy, use the sorted() function:
prices = [4.25, 1.25, 3.75, 2.00] sorted_prices = sorted(prices) print(prices, sorted_prices)
The first list stays the same. The reverse() method does not sort. It only flips the current order.
Strings sort by character code, so capital letters come before lowercase letters:
items = ["tea", "Latte", "bagel"] print(sorted(items))
This prints ['Latte', 'bagel', 'tea']. To ignore case, sort with key=str.lower.
How do you search and count items in a list?
orders = ["tea", "toastie", "tea", "latte"] print(orders.count("tea")) print(orders.index("latte")) print("lemonade" in orders)
This prints 2, 3, and False. count tells you how often an item appears, and index gives its first position. To check whether an item is in a list, in is the most readable way. index raises a ValueError when the item is missing, so check with in first if you are not sure.
Why does sort() return None?
Methods that change a list in place return None. If you assign the result to a variable, you replace your list with None:
prices = [4.25, 1.25, 3.75]
prices = prices.sort()
print(prices)This prints None. The same happens with append, insert, extend, remove, and reverse. The right way is to call the method on its own line: prices.sort().
How do you copy a list in Python?
Methods change the list in place, so any other variable that points to the same list sees the change:
morning = ["tea", "bagel"] evening = morning evening.append("toastie") print(morning) backup = morning.copy() backup.append("latte") print(morning, backup)
evening = morning does not make a new list. It gives the same list a second name. Use copy() when you want an independent list. Adding to backup does not change morning.
What to read next
In the [list basics lesson](/learn/python/list-basics) you manage an order list with these methods and practice with exercises. If you want to know why two variables can change together, the [reference and copy lesson](/learn/python/reference-copy) explains it.
- [Python for loop](/blog/python-for-loop)
- [Python list comprehension](/blog/python-list-comprehension)
- [Python course](/courses/python)
- #python
- #lists
- #list methods