Hacker News new | ask | show | jobs
by hprotagonist 2070 days ago
sets are great. A slightly janky but very tidy way to know if a list of (hashable) things has any duplicates in python is to test

  len(set(mylist)) == len(mylist) 
which probably loses on big O to some more clever approaches but is just fine for small n, and very readable.

Other trivially answerable questions include “what’s in me but not in this other collection”, “what’s in both of us”, “what’s in exactly one of us”, ... etc.

3 comments

I've had numerous occasions where doing set() on a python list was demonstrably faster for very large n with an unknown distribution rather than iterating through and potentially exiting early. I've always just assumed set() is doing the equivalent thing, but in the underlying C rather than in python.
Yup, this is exactly why I went a Set. The fact that it can't contain duplicates is super handy
Yes. Any kind of membership question is very easy to answer with a set/dict data structure.