COLLECTIONSChapter 5 · Collections
List methods
Lists change in place with methods: .append(x) adds to the end, .insert(i, x) adds at a position, .remove(x) deletes the first match, .pop() removes and returns the last item, and .sort() puts the list in order. sorted(items) returns a new sorted list and leaves the original alone. x in items checks membership and items.index(x) finds a position.
Worked example
How it reads
.append,.insert,.removeand.popchange the list itself.pop()also hands back the item it removedsorted(planets)makes a new list,planets.sort()reorders the original
Common mistakes
planets = planets.sort()sets planets toNone:.sort()changes the list and returns nothing..remove(x)raises ValueError whenxis not in the list. Check withinfirst.

Cloud tip: Reach for
sorted() when you still need the original order later, and .sort() when you do not.

