Hacker News new | ask | show | jobs
by Waterluvian 2583 days ago
Sets are my #1 favourite Python built-in. I used to have these tedious imperative functions for doing change detection on collections. Then I learned sets and it turned into obvious code like:

added = b - a

removed = a - b

repeated = a & b

Etc.

Of course this is set theory and not Python specific. But still. Learn sets!

2 comments

Pycon 2019 about how to use Sets more in your every day code: https://www.youtube.com/watch?v=tGAngdU_8D8
frozenset is a really nice one for enforcing immutabolity :)
Frozensets can also be keys in a dictionary.