Loading lessons...
List Methods Reference
List Methods Reference
The full toolkit of methods on every list.
| Method | Job |
|---|---|
append() | Add at the end |
clear() | Remove all items |
copy() | Return a copy |
count() | Count occurrences |
extend() | Add all items of another collection |
index() | Find the index of a value |
insert() | Add at an index |
pop() | Remove and return an item |
remove() | Remove the first match by value |
reverse() | Flip the order |
sort() | Sort in place |
Examples
nums = [1, 2, 2, 3]
print(nums.count(2)) # 2
print(nums.index(3)) # 3
Quick checks
count()returns how many times a value appears.index()returns the position of the first match.pop()without an argument removes the last item.remove()raises ValueError if the value isn't there.
TL;DR
- append, extend, insert add items.
- remove, pop, clear remove items.
- index, count, sort, reverse, copy inspect or reorganize.
- These are the most-used list methods.