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.

Easy Python 3.9+ Aug 9, 2026 Dictionaries & sets 12 views 0 copies

Python code

30 lines
Python 3.9+
def 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

stdout
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

  1. Use a regular `for` loop to build the dictionary instead of `dict(zip(...))` for clarity
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Dictionaries & sets

Related tutorials and quizzes for this topic.