Sets

Sets in Python

In Python, a set is an unordered collection of unique elements, meaning it automatically removes duplicate data, which makes sets especially useful for tasks like removing deduplication. Converting a list to a set and back again is a quick way to remove duplicate values.

# Convert a list to a set and then back to a list to remove duplicates
(list(set(my_list)))

Sets are defined using curly braces ({}) or the set() constructor, and they support a variety of mathematical operations for combining sets.

  • set1.union(set2) Returns all elements from set 1 and 2, removing duplicates.
  • set1.intersection(set2) Returns elements that are present in both sets.
  • set1 - set2 or set1.difference(set2) Returns elements in set1 that are not in set2.
  • set1.symmetric_difference(set2) Returns elements that are in either set, but not in both.

Sets are also efficient for membership testing (x in my_set), typically faster than lists due to their underlying hash table implementation. However, since sets are unordered, they don't support indexing or slicing. In recent versions of Python, sets can only contain immutable (hashable) types, so items like tuples can be added, but lists and dictionaries cannot. Python also provides a frozenset type, which is an immutable version of a set, useful when you need a set that itself can be used as a key in another dictionary or added to another set.