← VCE Software Development U3O1

Lists – Sorting with sorted() and .sort()

sorted(x) returns a new sorted list and leaves x unchanged. x.sort() sorts the list x itself (in place) and returns None. Both accept reverse=True and key= to change the order.

Understanding Lists – Sorting with sorted() and .sort() in VCE Software Development U3O1

What it does: Adding reverse=True flips the normal ascending order so the biggest value comes first. The key= function picks which value from each dictionary is used to decide the order, here the age. sorted() returns a new sorted list and leaves the original list unchanged. .sort() sorts the list in place, so the original list itself is changed. key= needs the function itself (len), not a call to it. Python calls the function on each item for you. Assigning the result of .sort() throws away the list because the method returns None. The new sorted list from sorted() must be stored in a variable, otherwise it is lost.

Common mistakes: Lists have no .sorted() method. sorted() is a built-in function that takes the list as an argument, so the line should be ranked = sorted(scores). Round brackets make a tuple, and tuples have no .sort() method, so line 2 would crash. The list needs square brackets: names = ['Zed', 'Amy', 'Bo'].

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 142–155) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
scores = [70, 55, 90] result = sorted(scores) What happens to the original list scores?
It stays [70, 55, 90]It becomes [55, 70, 90]
Matches
scores = scores.sort()
↓
.sort() changes the list in place and returns None, so scores becomes None. Use scores.sort() on its own, or scores = sorted(scores).
Applying
Meanings
Term
sorted() and .sort()
Meaning
sorted(x) returns a new sorted list and leaves x unchanged. x.sort() sorts the list x itself (in place) and returns None. Both accept reverse=True and key= to change the order.
Practice
scores = [72, 95, 88, 61]
Testing
Gaps
ordered = ([70, 55, 90])
Check Answer
Spots
1scores = [72, 95, 58, 80]
2ranked = scores.sorted()

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Peregrine Falcon' badge — Operator Ace, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. scores = [70, 55, 90] result = sorted(scores) What happens to the original list scores?
A. It becomes [55, 70, 90]
B. It stays [70, 55, 90] ✓
sorted() returns a new sorted list and leaves the original list unchanged.
2. scores = [70, 55, 90] scores.sort() What happens to the original list scores?
A. It becomes [55, 70, 90] ✓
B. It stays [70, 55, 90]
.sort() sorts the list in place, so the original list itself is changed.
3. Which line gives a variable called ranked a sorted list?
A. ranked = sorted(scores) ✓
B. ranked = scores.sort()
.sort() returns None, so ranked would be None. sorted() returns the new sorted list.
4. Which line sorts a list called names from Z to A?
A. names.sort(reverse=True) ✓
B. names.sort(reverse=False)
reverse=True flips the order to descending. reverse=False is the default, which sorts A to Z.
5. Which line sorts words from shortest to longest?
A. sorted(words, key=len())
B. sorted(words, key=len) ✓
key= needs the function itself (len), not a call to it. Python calls the function on each item for you.

Match the Pairs

new_list = sorted(scores) → Makes a new sorted list and leaves the original scores list unchanged
scores.sort() → Sorts the scores list itself (in place) and returns None
Sort a list from highest to lowest using reverse=True → sorted(scores, reverse=True)
Sort a list of dictionaries by one value using key= → students.sort(key=lambda s: s["age"])
scores = scores.sort() → .sort() changes the list in place and returns None, so scores becomes None. Use scores.sort() on its own, or scores = sorted(scores).
sorted(names) print(names) → sorted() does not change names. It returns a new list that was never saved, so names prints in the old order. Use names = sorted(names) or names.sort().

Key Term

sorted() and .sort()
sorted(x) returns a new sorted list and leaves x unchanged. x.sort() sorts the list x itself (in place) and returns None. Both accept reverse=True and key= to change the order.

Fill in the Blanks

1. ordered = sorted([70, 55, 90]) print(ordered)
Hint: Type the built-in function that returns a brand new ordered list and leaves the original alone.
2. scores = [70, 55, 90] scores.sort() print(scores)
Hint: Type the list method that rearranges the original list itself and returns None.
3. names = ['Zed', 'Amy', 'Bo'] names.sort(reverse=True) print(names)
Hint: Type the keyword argument name that flips the order so it goes from Z to A.
4. words = ['kiwi', 'fig', 'banana'] print(sorted(words, key=len))
Hint: Type the keyword argument name that tells Python which value to compare for each item.
5. people = [{'age': 15}, {'age': 12}] people.sort(key=lambda p: p['age']) print(people)
Hint: Type the list method that rearranges the list of dictionaries in place by age.

Spot the Error

Question 1
A scores = [72, 95, 58, 80]
B ranked = scores.sorted()
C print(ranked)
D print(scores)
Line B — Lists have no .sorted() method. sorted() is a built-in function that takes the list as an argument, so the line should be ranked = sorted(scores).
Question 2
A names = ('Zed', 'Amy', 'Bo')
B names.sort()
C print(names)
Line A — Round brackets make a tuple, and tuples have no .sort() method, so line 2 would crash. The list needs square brackets: names = ['Zed', 'Amy', 'Bo'].
Question 3
A nums = [4, 9, 1, 7]
B print(nums)
C sorted_nums = nums.sort()
D print(sorted_nums)
Line C — .sort() sorts the list in place and returns None, so sorted_nums would be None. To get a new sorted list, use sorted_nums = sorted(nums).
Question 4
A def by_len(w)
B return len(w)
C words = ['pear', 'fig', 'ox']
D words.sort(key=by_len)
Line A — A function definition must end with a colon. It should be def by_len(w): so the key function can be used to sort by length.
Question 5
A pts = [60, 85, 72]
B pts.sort(reverse=true)
C print(pts[0])
Line B — Python's Boolean value is True with a capital T. Lowercase true is not defined and causes a NameError. It should be pts.sort(reverse=True).

Student with a class code? Join your class →