Welcome to Day 13 of the #80DaysOfChallenges journey! Today’s challenge felt like a quiet little puzzle with real-world roots: finding common friends between two people’s lists. It’s the kind of problem you’d run into in social apps, recommendation systems, or even debugging access control. Simple on the surface, but a perfect excuse to flex Python’s set type and think about efficiency without overcomplicating things.
💡 Key Takeaways from Day 13: Common Friends with Sets
The core task was to take two lists of names say, Paul’s friends and Tina’s friends, and return the names that appear in both. No duplicates, no noise, just the overlap. We used sets to make it fast, clean, and intuitive. Let’s walk through the three pillars of this solution: conversion, intersection, and output clarity.
1. From Lists to Sets: Eliminating Duplicates and Speeding Up Lookups
Lists are great for ordered data, but they’re slow at answering “is X in here?”, that’s O(n) per check. Sets? O(1) average. Plus, they automatically deduplicate. Converting the input lists to sets was the first smart move:
paul_set = set(paul_friends)
tina_set = set(tina_friends)
Even if someone accidentally added “Tim” twice to Tina’s list, the set silently cleans it up. No extra code needed. It’s one of those small Python wins that feels almost magical when you first see it in action.
2. Intersection with &: The One-Liner That Does the Heavy Lifting
Once we have sets, finding common elements is literally one operator:
common = paul_set & tina_set
That & isn’t bitwise magic here—it’s set intersection. It returns a new set with only the elements present in both original sets. Behind the scenes, Python optimizes this to run in linear time relative to the smaller set. No nested loops, no manual tracking. Just pure, readable intent.
I remember smiling the first time I used this. It felt like cheating—in the best way.
3. Output That Makes Sense: Sorting and Empty-State Handling
Raw sets are unordered, so we sort before printing. And we don’t want to print an empty line if there’s no overlap, so we check:
if common:
print(f"Common friends: {', '.join(sorted(common))}")
else:
print("No common friends found.")
The sorted(common) gives us alphabetical order (great for consistency), and ', '.join() turns the list into a natural, readable string. Little touches like the newline \n and clear phrasing make the output feel polished, not just functional.
🎯 Summary and Reflections
This challenge was deceptively rich. On paper, it’s “find duplicates across two lists.” In practice, it taught me:
- Efficiency matters early: Sets turn a potential O(n×m) brute-force scan into O(n + m).
-
Readability is power:
set(a) & set(b)says everything in four characters. - Real-world relevance: This pattern shows up in user matching, tag overlap, permission checks, and more.
What surprised me was how satisfying the empty-case message felt. “No common friends found.”—it’s polite, clear, and prevents confusion. It’s a reminder that good code isn’t just correct; it communicates.
I also started thinking: what if we had 5 people? 50? The same & operator chains beautifully: set1 & set2 & set3. Or use set.intersection() for dynamic lists. The foundation scales.
Advanced Alternatives: Want to go further? Return not just names, but how many mutual connections? Or build a friendship graph with networkx? Or make it interactive with user input? The door’s wide open. How would you extend this for a real social feature? Let me know below!
🚀 Next Steps and Resources
Day 13 was a masterclass in doing more with less. Sets aren’t flashy, but they’re foundational. If you’re following #80DaysOfChallenges, did you use sets? Or did you roll your own logic? Any edge cases you considered (like case sensitivity in names)? Share your take!
- Source Code for Challenge #13: scripts/common_friends.py
- Main Repository: 80-days-of-challenges
- Daily Updates: Twitter/X (@Shahrouzlogs)
Onward to Day 14—can’t wait to see what’s next!
Top comments (0)