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

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

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

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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]
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]
3. Which line gives a variable called ranked a sorted list?
A ranked = sorted(scores)
B ranked = scores.sort()
4. Which line sorts a list called names from Z to A?
A names.sort(reverse=True)
B names.sort(reverse=False)
5. Which line sorts words from shortest to longest?
A sorted(words, key=len())
B sorted(words, key=len)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1new_list = sorted(scores) A.sort() changes the list in place and returns None, so scores becomes None. Use scores.sort() on its own, or scores = sorted(scores).
2scores.sort() BMakes a new sorted list and leaves the original scores list unchanged
3Sort a list from highest to lowest using reverse=True Csorted() 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().
4Sort a list of dictionaries by one value using key= Dstudents.sort(key=lambda s: s["age"])
5scores = scores.sort() ESorts the scores list itself (in place) and returns None
6sorted(names) print(names) Fsorted(scores, reverse=True)

Definitions

sorted() and .sort()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
ordered = ______([70, 55, 90]) print(ordered)
2.
scores = [70, 55, 90] scores.____() print(scores)
3.
names = ['Zed', 'Amy', 'Bo'] names.sort(_______=True) print(names)
4.
words = ['kiwi', 'fig', 'banana'] print(sorted(words, ___=len))
5.
people = [{'age': 15}, {'age': 12}] people.____(key=lambda p: p['age']) print(people)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A scores = [72, 95, 58, 80]
B ranked = scores.sorted()
C print(ranked)
D print(scores)
Question 2
A names = ('Zed', 'Amy', 'Bo')
B names.sort()
C print(names)
Question 3
A nums = [4, 9, 1, 7]
B print(nums)
C sorted_nums = nums.sort()
D print(sorted_nums)
Question 4
A def by_len(w)
B return len(w)
C words = ['pear', 'fig', 'ox']
D words.sort(key=by_len)
Question 5
A pts = [60, 85, 72]
B pts.sort(reverse=true)
C print(pts[0])

Answer Key

Lists – Sorting with sorted() and .sort()
Section A: Quick Questions
Q1
B — It stays [70, 55, 90]
Q2
A — It becomes [55, 70, 90]
Q3
A — ranked = sorted(scores)
Q4
A — names.sort(reverse=True)
Q5
B — sorted(words, key=len)
Section B: Match the Pairs
Q1
new_list = sorted(scores) → Makes a new sorted list and leaves the original scores list unchanged
Q2
scores.sort() → Sorts the scores list itself (in place) and returns None
Q3
Sort a list from highest to lowest using reverse=True → sorted(scores, reverse=True)
Q4
Sort a list of dictionaries by one value using key= → students.sort(key=lambda s: s["age"])
Q5
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).
Q6
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().
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
ordered = sorted([70, 55, 90]) print(ordered)
Q2
scores = [70, 55, 90] scores.sort() print(scores)
Q3
names = ['Zed', 'Amy', 'Bo'] names.sort(reverse=True) print(names)
Q4
words = ['kiwi', 'fig', 'banana'] print(sorted(words, key=len))
Q5
people = [{'age': 15}, {'age': 12}] people.sort(key=lambda p: p['age']) print(people)
Section E: Spot the Error
Q1
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).
Q2
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'].
Q3
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).
Q4
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.
Q5
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).