Members of Exactly One Club
Advanced · Stretch · from Python Sets: Unique Collections, Operations, and Use Cases
From the lesson: Python Sets: Unique Collections, Operations, and Use Cases
Imagine you're a teacher taking attendance. A student accidentally signs the attendance sheet twice. Does that mean they're in class twice? Of course not. You only care about who showed up, not how many times they wrote their name.
Read the lessonA school keeps a dictionary of clubs: each key is a club and each value is a list of its members (a list may name someone twice by mistake). Write a function in_one_club(clubs) that returns, as a sorted list, the people who belong to exactly one club.
in_one_club({"chess": ["ana", "ben"], "drama": ["ben", "cal"]}) # ['ana', 'cal']ben is in two clubs, so the answer leaves ben out. There can be any number of clubs. Try to solve it with set operations.
Return the answer instead of printing it: Check calls in_one_club() with its own clubs, some of them hidden.
What Check runs
Your code runs first, then each of these lines, and 5 more that stay hidden. The output must match exactly.
| Runs | Expected output |
|---|---|
| in_one_club({"chess": ["ana", "ben"], "drama": ["ben", "cal"]}) | ['ana', 'cal'] |
| in_one_club({"art": ["dee"], "music": ["eve", "dee", "fay"]}) | ['eve', 'fay'] |
More Set exercises
- Remove Duplicates from a ListBeginner
- Predict the Set OutputBeginner
- Find Students in Both ClassesIntermediate
- Fix the Empty Set BugIntermediate
- Find Missing IngredientsIntermediate
- Deduplicate While Preserving OrderAdvanced
All Python exercises · 50 practice problems · Data Structures quiz