A Python list is an ordered, changeable collection of items. Lists have 11 built-in methods that let you add, remove, find, count, sort, reverse, and copy items. Because lists are mutable, most of these methods change the list in place instead of creating a new one, which is the single most important thing to remember when using them.
In this tutorial, you’ll learn every list method with examples, see which built-in functions complement them, and avoid the mistakes that trip up beginners.
Key terms used in this article:
- List: An ordered, mutable sequence written with square brackets, such as
[1, 2, 3]. - Mutable: Can be changed after it’s created.
- In place: The method changes the original list and returns
None, instead of returning a new list. - Index: The position of an item. The first item is at index
0, and the last is at-1.
Table of contents
List methods at a glance
| Method | Description | Returns |
|---|---|---|
append(item) | Adds one item to the end | None |
extend(iterable) | Adds every item from an iterable to the end | None |
insert(index, item) | Inserts an item at a given position | None |
remove(item) | Removes the first matching item | None |
pop(index=-1) | Removes and returns the item at an index (the last item by default) | The removed item |
clear() | Removes all items | None |
index(item, start, end) | Returns the index of the first matching item | int |
count(item) | Counts how many times an item appears | int |
sort(key=None, reverse=False) | Sorts the list in place | None |
reverse() | Reverses the list in place | None |
copy() | Returns a shallow copy of the list | New list |
You can list them from Python with [m for m in dir(list) if not m.startswith("_")].
Adding items to a list
append()
append() adds a single item to the end of the list:
Output:
['apple', 'banana', 'cherry'] ['apple', 'banana', 'cherry', ['kiwi', 'mango']] 4
append() is the fastest way to grow a list. Building a list in a loop usually looks like this:
Output:
[1, 4, 9, 16, 25]
extend()
extend() adds each item from an iterable (a list, tuple, string, set, range, and so on):
Output:
['apple', 'banana', 'kiwi', 'mango'] ['apple', 'banana', 'kiwi', 'mango', 'fig'] ['a', 'b', 'c', 'd']
append() vs. extend() vs. +
Output:
[1, 2, [3, 4]] [1, 2, 3, 4] [1, 2] [1, 2, 3, 4] [1, 2, 3, 4]
insert()
insert(index, item) puts an item before the given index:
Output:
['red', 'yellow', 'green', 'blue'] ['black', 'red', 'yellow', 'green', 'blue'] ['black', 'red', 'yellow', 'green', 'blue', 'white'] ['black', 'red', 'yellow', 'green', 'blue', 'white', 'pink']
Inserting near the start of a long list is slow, because every item after it has to move. If you often add items at the front, use collections.deque and its appendleft() method.
Removing items from a list
remove()
remove(item) deletes the first item equal to the value. It raises ValueError if the item isn’t in the list:
Output:
['cat', 'parrot', 'dog'] Error: list.remove(x): x not in list
To remove every occurrence of a value, build a new list with a comprehension:
Output:
[1, 3, 4]
pop()
pop() removes an item by index and returns it. Without an argument, it removes the last item:
Output:
d ['a', 'b', 'c'] a ['b', 'c'] Error: pop from empty list
pop() from the end is fast, which makes a list a good stack (last in, first out):
Output:
Back to: page3 Back to: page2 ['page1']
clear()
clear() empties the list but keeps the same list object:
Output:
[] []
The del statement
del isn’t a method, but it’s the other common way to remove items, including whole slices:
Output:
[1, 2, 3, 4, 5, 6] [1, 4, 5, 6] [4, 6]
Which removal tool should you use?
| You want to | Use |
|---|---|
| Remove by value | remove(value) |
| Remove by index and get the item | pop(index) |
| Remove by index or slice, without getting the item | del lst[i] or del lst[a:b] |
| Remove every matching value | A list comprehension |
| Remove everything | clear() |
Finding and counting items
index()
index(item) returns the position of the first match, and raises ValueError if the item isn’t found. Optional start and end arguments limit the search:
Output:
1 3 Error: 'z' is not in list
To find all positions of a value, use enumerate():
Output:
[1, 3]
count()
Output:
3 0
Calling count() for every distinct value is slow on large lists. Use collections.Counter to count everything in one pass:
Output:
Counter({'yes': 3, 'no': 1, 'maybe': 1})
[('yes', 3)]
Sorting and reversing
sort()
sort() sorts the list in place in ascending order. Use reverse=True for descending order:
Output:
[3, 7, 19, 25, 42] [42, 25, 19, 7, 3] ['Brad', 'Emma', 'adam', 'kelly']
Sorting with a key function
The key argument takes a function that’s called on each item. The list is sorted by the values it returns:
Output:
['adam', 'Brad', 'Emma', 'kelly']
['adam', 'Brad', 'Emma', 'kelly']
[('Kelly', 95), ('Jessa', 88), ('Emma', 79)]
Python’s sort is stable: items that compare equal keep their original order. That lets you sort by several keys in steps, or return a tuple from the key function. See Lambda Functions in Python for more key examples.
Sorting a list that mixes incompatible types, such as numbers and strings, raises a TypeError.
sort() vs. sorted()
list.sort() | sorted(iterable) | |
|---|---|---|
| Works on | Lists only | Any iterable |
| Changes the original | Yes | No |
| Returns | None | A new sorted list |
Output:
[3, 1, 2] [1, 2, 3] None [1, 2, 3]
reverse()
reverse() flips the order of the list in place. It doesn’t sort:
Output:
[9, 3, 5, 1] [1, 5, 3, 9] [1, 5, 3, 9]
Copying a list
copy()
Assigning a list to another variable doesn’t copy it. Both names point to the same list:
Output:
[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3]
copy(), list(a), and a[:] all create a shallow copy: a new outer list that holds the same inner objects.
Shallow copy vs. deep copy
For nested lists, a shallow copy still shares the inner lists. Use copy.deepcopy() to copy everything:
Output:
[[99, 2], [3, 4]] [[1, 2], [3, 4]]
Built-in functions that work with lists
These aren’t list methods, but you’ll use them with lists all the time:
Output:
5
404
61 95
80.8
[61, 72, 88, 88, 95]
True True
[(0, 72), (1, 88)]
[('a', 72), ('b', 88)]
True
Performance of list methods
| Operation | Speed |
|---|---|
append(), pop() from the end | Fast, constant time |
lst[i] (read or write by index) | Fast, constant time |
insert(0, x), pop(0) | Slow for long lists: every item moves |
remove(), index(), count(), x in lst | Slow for long lists: searches item by item |
sort() | O(n log n) |
If you check membership often, convert the list to a set. If you add and remove at both ends, use collections.deque.
Real-world example: a simple to-do manager
Output:
To-do: ['Fix urgent bug', 'Write report', 'Email client', 'Book flights', 'Pay bills'] Completed: Fix urgent bug Remaining (A-Z): ['Email client', 'Pay bills', 'Write report'] Position of 'Pay bills': 1
Common mistakes
- Assigning the result of an in-place method.
numbers = numbers.sort()setsnumberstoNone. Callnumbers.sort()on its own line, or usesorted(). - Using
append()when you meanextend().a.append([3, 4])adds a nested list. - Removing items while looping over the same list. This skips items. Loop over a copy or build a new list.
- Thinking
b = acopies a list. Usea.copy(). - Calling
remove()orindex()without checking. Both raiseValueErrorwhen the item is missing.
Output:
[4, 5, 8] [5]
Practice exercises
Exercise 1: Remove duplicates from a list while keeping the original order.
Output:
[3, 1, 2, 4] [3, 1, 2, 4]
Exercise 2: Move all zeros in a list to the end while keeping the order of the other items.
Output:
[3, 1, 7, 5, 0, 0, 0]
Exercise 3: Sort a list of words by length, then alphabetically for words of the same length.
Output:
['fig', 'date', 'kiwi', 'pear', 'plum', 'apple']
Summary
- Lists have 11 methods:
append(),extend(),insert(),remove(),pop(),clear(),index(),count(),sort(),reverse(), andcopy(). - Most methods change the list in place and return
None. Onlypop(),index(),count(), andcopy()return a value. - Use
append()for one item andextend()for many. Useremove()to delete by value andpop()ordelto delete by index. - Use
sort()to sort in place andsorted()to get a new list. Passkey=to control the order. b = adoesn’t copy a list. Usecopy()for a shallow copy andcopy.deepcopy()for nested lists.
Also, See:

Leave a Reply