List methods change the list
Not met yet
.remove(x) takes out the first x, and crashes with a ValueError if there isn't one. .insert(i, x) puts x at position i and moves the rest along. .count(x) says how many there are, and .index(x) where the first one is. The methods that change a list hand back None.
Try it
main.pyshopping = ["flour", "eggs", "flour"]
shopping.remove("flour")
shopping.insert(0, "yeast")
print(shopping)
print(shopping.count("flour"), shopping.index("eggs"))
Output
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks .remove(x) takes out every copy of x, not just the first.
- Thinks .remove(x) takes out the last copy of x rather than the first.
- Thinks .remove() takes a position, like .pop(), rather than the value to find.
- Thinks .remove(x) quietly does nothing when x is not in the list, rather than raising a ValueError.
- Thinks .insert(i, x) overwrites the item at position i rather than moving the rest along.