Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python sorting code

Practice sorting iterables, using key functions, ordering multiple fields, and choosing list.sort().

Beginner ~6 min read

Challenge 1

Create a new sorted list.

python Your answer
values = [4, 1, 3, 2]
result = 
print(result)

Press Ctrl⌘Enter to check your answer

Challenge 2

Sort the words without treating uppercase and lowercase as different ordering keys.

python Your answer
words = ["pear", "Apple", "banana", "fig"]
result = 
print(result)

Press Ctrl⌘Enter to check your answer

Challenge 3

Sort first by age and then by name.

python Your answer
people = [{"name": "Mina", "age": 30}, {"name": "Ali", "age": 22}, {"name": "Sara", "age": 22}]
result = 
for person in result:
    print(person["age"], person["name"])

Press Ctrl⌘Enter to check your answer

Challenge 4

Sort the existing list in descending order in place.

python Your answer
items = [4, 1, 3, 2]

print(items)

Press Ctrl⌘Enter to check your answer