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 |
|---|---|---|---|
|
| Adds |
|
|
| Appends every item from |
|
|
| Inserts |
|
|
| Removes the first item equal to |
|
|
| Removes and returns the item at index | The removed item |
|
| Removes every item, leaving the list empty. |
|
|
| Returns the index of the first item equal to |
|
|
| Counts how many times |
|
|
| Sorts the list in place using |
|
|
| Reverses the order of items in place. |
|
|
| Returns a shallow copy of the list — equivalent to | A new |
append, extend, and insert
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]
remove, pop, and clear
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
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.
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.
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"]) # Gracenumbers = [3, 1, 2] numbers.reverse() print(numbers) # [2, 1, 3] - just reverses order, does NOT sort
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.
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!