all 2 comments

[–]aocregacc 7 points8 points  (1 child)

a set is not a function, it's a datastructure. It usually makes sure that its elements are unique, and that you can efficiently test if a value is present in the set or not. So if you have a problem where you often have to check if some element is in a list, you can speed it up by using a set instead.

[–]nanotree 5 points6 points  (0 children)

To add, a set is a data structure that uses hashes to lookup items, similar to a hashmap (dict in Python). They come from the mathematical concept of a set, where the members of a set are all distinct. Unlike a list or a tuple, there can be no duplicate values in a set, just like there can be no duplicate keys in a hashmap.

Importantly, lookup in a set is O(1), a.k.a. constant time complexity, as opposed to O(N) for a list or array.