Sort Students by GPA
Intermediate · Write the code · from Python Lambda Functions: When to Use (and When Not To)
From the lesson: Python Lambda Functions: When to Use (and When Not To)
Imagine you need to leave someone a quick note: "Back in 5 minutes." You wouldn't write a formal letter with a greeting, three paragraphs, and a signature. You'd scribble it on a sticky note and be done.
Read the lessonGiven a list of student tuples (name, gpa), sort them by GPA from highest to lowest using sorted() with a lambda. Print each student on a separate line in the format name: gpa.
Use this list:
students = [('Alice', 3.5), ('Bob', 3.9), ('Charlie', 3.2), ('Diana', 3.7)]Expected output:
Bob: 3.9
Diana: 3.7
Alice: 3.5
Charlie: 3.2students = [('Alice', 3.5), ('Bob', 3.9), ('Charlie', 3.2), ('Diana', 3.7)]
# Sort by GPA (highest first) and print
More Lambda exercises
- Predict the Lambda OutputBeginner
- Sort by Last CharacterBeginner
- Filter and TransformIntermediate
- Sort Words by LengthAdvanced
All Python exercises · 50 practice problems · Functions quiz