Kodewick
Start learning →
Mini challenge←→0 / 100

Group by class

#065Mini challengeDictsLists6 min

The sports day sign-up sheet is a list of [name, class] pairs. Write group_by_class(students) to return a dict of class to a list of its names, with classes and names in the order they appear on the sheet. An empty sheet gives {}.

Examples
group_by_class([["Ann", "3A"], ["Bo", "3B"], ["Cy", "3A"]])→{'3A': ['Ann', 'Cy'], '3B': ['Bo']}
group_by_class([["Dee", "4C"]])→{'4C': ['Dee']}
main.py
def group_by_class(students):
    groups = {}
    for name, cls in students:
        # your code here: a new class gets an empty list,
        # then add the name to it
        pass
    return groups
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.