Learning outcomes
By the end of this lecture, you should be able to:- use major list methods
- explain in-place modification
- avoid aliasing mistakes
- distinguish
sort()fromsorted()
Major list methods
append(x)extend(iterable)insert(i, x)remove(x)pop([i])sort()reverse()count(x)index(x)
sort() vs sorted()
In-place trap
Aliasing trap
Nested list example
Exam hints and traps
appendadds one itemextendadds multiple items from iterableremove(x)removes by valuepop(i)removes by index and returns itemsort()returnsNone
Quick practice
- Add
50to end of[10, 20]. - Remove first occurrence of
3from[3, 1, 3, 5]. - Predict:
Answer key
[1, 2, 3]
