Set

An unordered collection of unique items, written in braces, e.g. {1, 2, 3}.

A set stores unique values — no duplicates — with no fixed order. It is built for fast membership tests (x in s) and set maths: union, intersection, and difference. Create one with braces or set(...). Note that {} makes an empty dictionary, so use set() for an empty set.

Example
emails = ["[email protected]", "[email protected]", "[email protected]"]
unique = set(emails)         # duplicates dropped
print(len(unique))
print("[email protected]" in unique)   # fast membership test
Output
2
True

Where this shows up in real Python

Sets are perfect for removing duplicates, testing membership quickly, and comparing two collections — who is in list A but not list B.

Commonly used Set tools

  • .add(x) — add an item
  • .discard(x) — remove without error if missing
  • a | b — union — items in either
  • a & b — intersection — items in both
  • a - b — difference — in a but not b
  • set(items) — drop duplicates from a list

Official documentation: Python Library Reference: Set Types

Related lessons

Related terms