Club Grouping with setdefault()
A school office is sorting sign-up slips for after-class clubs. Each slip contains a club name and one student name. The office wants the final result organized as a dictionary where every club points to the list of students who joined it.
The order inside each club list should follow the original arrival order of the slips. That means if the chess club receives "Mina" first and "Dika" later, the stored list should be ["Mina", "Dika"].
For example, if the slips are [["Chess", "Mina"], ["Art", "Rafi"], ["Chess", "Dika"]], the grouped result should be {"Chess": ["Mina", "Dika"], "Art": ["Rafi"]}. If there are no slips at all, the answer should be an empty dictionary.
This is a grouping problem rather than a counting problem. The dictionary keys are club names, but the values are lists that keep the student names collected for each club.
Example Input & Output
No sign-up slips means there are no groups to build.
Students are grouped by club and keep their original sign-up order.
A single sign-up creates one club key with one student in its list.
Algorithm Flow

Solution Approach
This problem is a nice demonstration of how dictionaries and lists can work together. The dictionary decides which club bucket to use, and the list attached to that club keeps the student names in order.
The Python method setdefault() is useful here because it solves the "first time we see this key" problem in a tidy way. If the club is not already in the dictionary, setdefault() creates it with a default value. In this case, that default value should be an empty list.
That gives a compact solution:
The update line is doing two jobs at once. First it guarantees that groups[club] exists and is a list. Then it appends the current student to that list. When the same club appears again later, the existing list is reused and the new student is added to the end.
This is exactly the right behavior for the sign-up desk. The club names become the grouping keys, while the student names are collected in the order their slips arrive. No sorting is needed, and no extra pass is required after the loop finishes.
You could write the same logic with an if club not in groups check before appending. That is still correct, but setdefault() is the method-focused idea this problem is trying to teach. It keeps the dictionary-initialization step close to the append step, which makes the grouping pattern easier to recognize.
The runtime is O(n) for n slips because each sign-up is processed once. The extra space is proportional to the size of the output dictionary and its collected name lists. For a beginner-friendly grouping problem, this is a very natural Python pattern to learn early.
Best Answers
def group_club_slips(slips):
groups = {}
for club, student in slips:
if club not in groups:
groups[club] = []
groups[club].append(student)
return groupsComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
