Lists in both languages
After this: Read the Python and Java spellings side by side and use the built-in helpers.
On this page · 7 sections · 3 checks
So far you have read one language at a time. This lesson puts the two side by side, because when you look for help on the web you will meet both, and because the Java list has a different name: ArrayList. The ideas are the same in both. The spellings differ, and in a few places the behaviour differs too.
The same list, both spellings
| What you want | Python | Java |
|---|---|---|
| Make a list | [72, 85, 60] | new ArrayList<>(List.of(72, 85, 60)) |
| Read position 1 | marks[1] | marks.get(1) |
| Add at the end | marks.append(91) | marks.add(91) |
| Put at position 0 | marks.insert(0, 50) | marks.add(0, 50) |
| Take out the value 60 | marks.remove(60) | marks.remove(Integer.valueOf(60)) |
| Replace position 1 | marks[1] = 75 | marks.set(1, 75) |
| Length | len(marks) | marks.size() |
| Largest value | max(marks) | Collections.max(marks) |
| Total | sum(marks) | a loop, as in the last lesson |
| Sort the list itself | marks.sort() | Collections.sort(marks) |
| A sorted copy | sorted(marks) | copy it, then sort the copy |
len, max, sum and sorted in Python, and size, Collections.max and Collections.sort in Java, are : lines that come with the language and do a job you could also write as a loop. Java's Collections needs one more line at the top of the file, import java.util.Collections;.
The last value
Python can count from the end: position -1 is the last value, -2 the one before it. Java has no negative positions. The way that works in both languages is the length minus one.
marks = [72, 85, 60, 91]print(marks[3])print(marks[-1])print(marks[len(marks) - 1])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
All three lines print 91. The first names position 3 directly. The second is the Python shortcut, marks[-1]which Java does not have, so the Java line is the third one again. The third, marks[len(marks) - 1]marks.get(marks.size() - 1), works in both.
The average, and a division to watch
The average mark is the total divided by the length. In "Your first program" you saw that dividing two whole numbers behaves differently in the two languages, and this is where it matters.
marks = [72, 85, 60, 91]total = 0for n in marks: total = total + naverage = total / len(marks)print(average)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
Python's / always keeps the fraction part, so 308 / 4 prints 77.0. In Java, / on two whole numbers throws the fraction part away, so the Java line turns the total into a double first: (double) total / marks.size(). Both print 77.0. Watch the box average in the trace: its small label says float in Python and double in Java, the kind of number that keeps a fraction part.
Python runs in the browser below; Java is traced above.
Change 91 to 92 and run again: the average should end in .25.
A sorted copy, or sorted in place
There are two ways to put a list in order, and they do different things. sorted(marks)Collections.sort on a copy gives you a new list in order and leaves marks as it was. marks.sort()Collections.sort(marks) changes marks itself: it works , and the old order is gone.
Java has only the in-place kind. For a sorted copy in Java, make the copy first with new ArrayList<>(marks) and sort that. In the trace, Java line 8 makes the copy and line 9 sorts it, so the trace marks line 9 for Python's line 2.
- Run the programRun it. Watch which box changes on line 2 and which box changes on line 5 (Java: lines 9 and 12).
marks = [72, 85, 60, 91]in_order = sorted(marks)print(in_order)print(marks)marks.sort()print(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
Java's fixed-size kind
Check yourself
marks holds [72, 85, 60, 91]. The line in_order = sorted(marks)Collections.sort(inOrder) runs, where in_orderinOrder is the new list sorted gives backa copy made with new ArrayList<>(marks). What does marks hold afterwards?
The total is 308 and the list has four values. The last line uses //, the whole-number division from Your first program/ on two whole numbers, which Your first program showed throws the fraction part away. What does this print?
marks = [72, 85, 60, 91]total = 308print(total // len(marks))ArrayList<Integer> marks = new ArrayList<>(List.of(72, 85, 60, 91));int total = 308;System.out.println(total / marks.size());marks holds [50, 72, 85, 60, 91]. Which line takes the value 60 out, so that the list becomes [50, 72, 85, 91]?
Honesty notes
- Read the Python and Java spellings of every list operation side by side.
- Reach the last value from the end in Python (position -1) and with size minus one in Java.
- Divide a total so that the average keeps its fraction part in both languages.
- Tell a sorted copy from sorting in place, and say that Java has only the in-place kind.