3 ms·
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(myl
by hprotagonist 6y 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.
- abhishekjha 6y agoYes. Any kind of membership question is very easy to answer with a set/dict data structure.
- cgriswald 6y agoI'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.
- root993 6y agoYup, this is exactly why I went a Set. The fact that it can't contain duplicates is super handy