Changing a list
After this: Put a value in, take one out, replace one, and say what two names for one list do.
On this page · 6 sections · 3 checks
Through a school term the list of marks keeps changing. A late test result arrives and has to go at the front. One mark was entered by mistake and has to go. One was marked wrongly and gets corrected. In the last lesson you added a value at the end. This lesson shows the other three changes.
Four ways to change a list
| Change | Python | Java | What moves |
|---|---|---|---|
| Add at the end | marks.append(91) | marks.add(91) | nothing |
| Put at a position | marks.insert(0, 50) | marks.add(0, 50) | the values from that position on move right |
| Take a value out | marks.remove(60) | marks.remove(Integer.valueOf(60)) | the values after it move left |
| Replace at a position | marks[1] = 75 | marks.set(1, 75) | nothing |
Two of the four name a position: putting in and replacing. One names a value: taking out finds the first 60 in the row, wherever it is. Keep the two apart; the last check of the lesson "Lists in both languages" is about exactly this.
The smallest example, by hand
Start with [72, 85, 60, 91].
- Put 50 at position 0. Every value moves one place to the right to make room:
[50, 72, 85, 60, 91]. - Take out the value 60. The values after it move one place to the left to close the gap:
[50, 72, 85, 91]. - Replace the value at position 1 with 75. Nothing moves; 72 becomes 75:
[50, 75, 85, 91].
Tap through the same three changes in the row of cells. In the picture you point at the cell to take out; in code you name the value.
- Insert 50 at index 0Put 50 at position 0. Count how many values move one place to the right to make room.
- Remove index 3
- Write 75 at index 1
- 072
- 185
- 260
- 391
- 4·
- 5·
- 6·
- 7·
Run an operation to begin.
The same changes in code
Each change is followed by a print, so you can check every row against the picture.
- Run the programRun it. After each change the program prints the whole list; check each printed row against the picture above.
marks = [72, 85, 60, 91]marks.insert(0, 50)print(marks)marks.remove(60)print(marks)marks[1] = 75print(marks)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The rule: putting in and taking out move the neighbours. Replacing does not. A list is one row, and the row stays packed: there are never gaps, and the length is always exactly the number of values.
Change the 0 on line 2 to 2 and run again: where does 50 land, and what else moves?
Two names for one list
Here is something that catches almost every beginner. The line b = a does not make a second list. It gives the one list a second name. From then on, a and b are two labels on the same row, and a change made through either name is seen through both.
Step through this. Line 38 adds 91 through b. Watch both boxes change on that one line, because there is only one list behind them.
a = [72, 85, 60]b = ab.append(91)print(a)print(b)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
When you want a separate list, say so: list(a)new ArrayList<>(a) makes a real copy with the same values. A change to the copy leaves the original alone.
a = [72, 85, 60]c = list(a)c.append(91)print(a)print(c)[72, 85, 60] [72, 85, 60, 91]
ArrayList<Integer> a = new ArrayList<>(List.of(72, 85, 60));ArrayList<Integer> c = new ArrayList<>(a);c.add(91);System.out.println(a);System.out.println(c);[72, 85, 60] [72, 85, 60, 91]
The trace band draws each name as its own box, and when the two names share a list the two boxes show the same row. Keep in mind that there is one list behind them, not two.
Check yourself
The list starts as [72, 85, 60, 91]. Then marks.insert(0, 50)marks.add(0, 50) runs, and then marks.remove(60)marks.remove(Integer.valueOf(60)). What does the list hold now?
What does this print?
marks = [72, 85, 60]marks[0] = 70marks.append(91)print(marks)ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60));marks.set(0, 70);marks.add(91);System.out.println(marks);Line 2 gives the list a second name. What does this print?
a = [1, 2, 3]b = ab.append(4)print(len(a))ArrayList<Integer> a = new ArrayList<>(List.of(1, 2, 3));ArrayList<Integer> b = a;b.add(4);System.out.println(a.size());Honesty notes
- Put a value at a position (insert in Python, add with a position in Java) and say which values move to make room.
- Take a value out by naming it (remove in Python, remove with Integer.valueOf in Java) and say which values move to close the gap.
- Replace the value at a position without moving anything else.
- Explain why giving a list a second name is not a copy, and make a real copy when you need one.