Course 5.1Lesson 2 of 5
A list can be changed. append adds an item at the end, insert puts one at a position, remove deletes the first match, pop takes the last item out and gives it back, and a position on the left of an equals sign replaces an item. sort puts the items in order and reverse turns them around, and both change the list itself and give back None. sorted gives a new sorted list and keeps the original. A string cannot be changed, and trying to do so gives a TypeError.
Try the code and the simulations beside the lesson.
After this lesson you can
sort() and with sorted(),A string is fixed: you can make a new string, but you cannot change one letter of the old one. A list is not like that. We can add items, remove items, replace them and put them in order. These actions are called methods, and we write them after a dot, as we did for strings.
| Method | What it does |
|---|---|
a.append(x) | adds x at the end |
a.insert(i, x) | puts x at position i, and the others move along |
a.remove(x) | deletes the first x |
a.pop() | takes the last item out and gives it back |
a.pop(i) | takes the item at position i out and gives it back |
a.sort() | puts the items in order, smallest first |
a.sort(reverse=True) | puts them in order, biggest first |
a.reverse() | turns the list around |
a[0] = 99 | replaces the item at position 0 |
This is the point that surprises beginners. a.append(4) does not give a new list. It changes a. The same is true of sort, reverse, insert, remove and pop.
Here is a related thing: these methods give back None, because their work is the change, not an answer. A very common mistake is a = a.sort(). It sorts the list, and then sets a to None. After that, a is gone!
The rule:
a.sort() changes a, and gives back None.sorted(a) gives a new list, and a stays.A string has no sort(), because a string cannot be changed. A list can.
A string cannot be changed. Look.
The word for "can be changed" is mutable, and for "cannot be changed" it is immutable. Lists are mutable, and strings are immutable. You will meet the word again.
a.remove("apple") removes the item with that value, the first one that it finds. It gives a ValueError if the value is not there.a.pop(2) removes the item at position 2, and gives it back.del a[2] also removes the item at position 2, and gives nothing back.Think about which one you need: do you know the value, or the position?
a = a.sort() or a = a.append(4). These give None. Write a.sort() on its own.remove() of a value that is not in the list. A ValueError. Ask with in first.insert() with the arguments the wrong way round. The position comes first, then the value.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
append, insert, remove, pop, sort and reverse change the list itself.None. Never write a = a.sort().sorted(a) is a new sorted list. The original stays.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
a after a = [3, 1, 2] and a.sort()?a after a = [1, 2] and a.append(3)?a = [3, 1, 2], then a = a.sort(), leave in a?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.