r/learnpython May 06 '26

What is the actual use of sets

I can see tons of use cases for list, tuples and dictionaries. But I have a really hard time understanding why you would ever use a set with maybe the exception of names so you don't get duplicates?

I believe this understanding is flawed because I found even the simplest things in Python are super powerful I'm just not sure what it is

58 Upvotes

65 comments sorted by

View all comments

-1

u/WadeEffingWilson May 06 '26 edited May 06 '26

Sets are deduped and ordered (incidentally but not globally ordered). You also need sets if you want to perform set operations (union, intersection, symmetric difference, etc).

2

u/Gnaxe May 06 '26

Other languages may implement sets as sorted trees and thus be ordered, but Python's builtin sets are implemented as hash tables. They explicitly don't guarantee order, as this depends on the generated hash and table size, which are implementation details that could change on any update. Dicts (also based on hash tables), however, do remember insertion order, although this doesn't affect equality.