Data Structures · Lists · lesson 3 of 10
List methods
about 12 minutes · free · runs in your browser
The methods worth knowing
| Method | What it does |
|---|---|
.append(x) | add x to the end |
.extend(other) | add every item from another list |
.insert(i, x) | add x at position i |
.remove(x) | delete the first x (error if absent) |
.pop() | remove and return the last item |
.pop(i) | remove and return item i |
.sort() | sort in place |
.reverse() | reverse in place |
.count(x) | how many times x appears |
.index(x) | position of the first x |
The important distinction: .sort() changes the list and returns None, while
sorted(list) leaves the original alone and hands back a new sorted list. The same
pairing exists for .reverse() and reversed().
Also useful: sum(), min(), max() and len() all take a list directly.
Your turn: write process(scores) that removes the lowest score and returns the
remaining scores sorted from high to low. Do not change the caller's list.
You start from this, and edit it in the browser:
def process(scores):
# Drop the lowest, return the rest sorted high to low.
pass