How to Build a Gradebook with Python Dictionaries and Sets
Create a gradebook dictionary from student names and grades, find top students with a set comprehension, and add extra credit with a dict comprehension.
Python code
30 linesdef build_gradebook(students, grades):
"""Create a dictionary mapping student names to their grades."""
return dict(zip(students, grades))
def find_top_students(gradebook, passing_grade=60):
"""Return a set of students with grades at or above the passing grade."""
return {name for name, grade in gradebook.items() if grade >= passing_grade}
def add_extra_credit(gradebook, bonus_points=5):
"""Create a new dictionary with bonus points added to each grade."""
return {name: grade + bonus_points for name, grade in gradebook.items()}
if __name__ == "__main__":
student_list = ["Alice", "Bob", "Charlie", "Diana"]
grade_list = [85, 58, 92, 73]
gradebook = build_gradebook(student_list, grade_list)
print("Original gradebook:", gradebook)
top_students = find_top_students(gradebook)
print("Students passing:", sorted(top_students))
boosted_grades = add_extra_credit(gradebook)
print("With extra credit:", boosted_grades)
all_names = set(gradebook.keys())
print("All students:", sorted(all_names))
Output
Original gradebook: {'Alice': 85, 'Bob': 58, 'Charlie': 92, 'Diana': 73}
Students passing: ['Alice', 'Charlie', 'Diana']
With extra credit: {'Alice': 90, 'Bob': 63, 'Charlie': 97, 'Diana': 78}
All students: ['Alice', 'Bob', 'Charlie', 'Diana']
How it works
The zip function pairs each student name with a corresponding grade, and dict() turns those pairs into a dictionary. A set comprehension {name for name, grade in gradebook.items() if grade >= passing_grade} collects only the keys that satisfy the condition, guaranteeing unique names. The dict comprehension {name: grade + bonus_points for name, grade in gradebook.items()} builds a new dictionary without mutating the original. Using comprehensions keeps the code concise and readable.
Common mistakes
- Assuming the keys of a dictionary are ordered when iterating, whereas sets are unordered
- Modifying the original gradebook instead of creating a new dictionary with extra credit
- Forgetting that `dict(zip(students, grades))` silently truncates if one list is longer
Variations
- Use a regular `for` loop to build the dictionary instead of `dict(zip(...))` for clarity
- Use `sorted(find_top_students(gradebook))` to print passing students in a stable alphabetical order
Real-world use cases
- Mapping user IDs to scores in a quiz platform to quickly compute per-user results.
- Filtering a set of eligible customer segments for a marketing campaign based on thresholds.
- Applying a bonus to employee ratings in a review system without altering the base data.
Sponsored
More from Dictionaries & sets
- Build a Case-Insensitive Dict with a Wrapper Class in Python medium
- Build a defaultdict histogram of categories in Python easy
- Build adjacency dict graph from edges in Python easy
- Build an OrderedDict insertion order demo in Python 3 easy
- Check Invertible Mapping for Duplicate Values in Python easy
- Compare Two Dictionaries in Python easy
Keep learning
Related tutorials and quizzes for this topic.