Mathematical Set Operations
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Python sets natively support mathematical set algebra. Operators like |, &, -, and ^ solve complex comparison and filtering tasks in a single line.
Union (|): all elements combined
Union is the set of every value that is in the first set, the second set, or both. Each value appears once. You write it as a | b.
The union of two sets contains all elements that appear in either set without duplicates.
frontend = {"HTML", "CSS", "JavaScript"}backend = {"Python", "SQL", "JavaScript"}fullstack = frontend | backendprint(sorted(fullstack))Example 1 · Merging two mailing lists
team_a = {"Asha", "Ravi", "Meena"}team_b = {"Ravi", "Kiran"}everyone = team_a | team_bprint(sorted(everyone))print(len(everyone))['Asha', 'Kiran', 'Meena', 'Ravi'] 4
What this code does
|joins the two sets into one.Raviis in both lists, but a set keeps him once.- So the combined list has 4 people, not 5.
The duplicate 'JavaScript' is included only once.
Intersection (&): shared elements
Intersection is the set of values that are in both sets at the same time. You write it as a & b, and it is empty when the sets share nothing.
The intersection contains only elements that exist in both sets.
shared = frontend & backendprint(shared)Example 2 · Customers who bought both products
bought_phone = {"C1", "C2", "C3", "C4"}bought_case = {"C3", "C4", "C5"}both = bought_phone & bought_caseprint(sorted(both))print(len(both))['C3', 'C4'] 2
What this code does
&keeps only the ids found in both sets.- Customers C3 and C4 bought the phone and the case.
- This replaces a nested loop with one short line.
The console displays {'JavaScript'}.
Difference (-) and Symmetric Difference (^)
Set difference a - b is the set of values in a that are not in b. Symmetric difference a ^ b is the values in exactly one of the two sets.
A - B yields elements in A but not in B. A ^ B yields elements in either A or B, but not both.
only_frontend = frontend - backendunique_to_one = frontend ^ backendprint(sorted(only_frontend))print(sorted(unique_to_one))Example 3 · Students who have not submitted
registered_students = {"Rohan", "Aditi", "Meera", "Dev"}submitted_assignment = {"Aditi", "Dev"}
pending_students = registered_students - submitted_assignmentprint("Pending submission:", sorted(pending_students))Pending submission: ['Meera', 'Rohan']
What this code does
- Subtracts submitted students from registered students using
-. - Identifies students who have not yet submitted:
['Meera', 'Rohan'].
Symmetric difference removes the common element 'JavaScript'.
Subset, superset and disjoint
Subset is a set whose values are all found inside another set. a <= b is True when a is a subset of b. Two sets are disjoint when they have no value in common.
Use <= to test a subset, >= for a superset, and isdisjoint() to test that two sets share nothing.
required = {"read", "write"}granted = {"read", "write", "delete"}print(required <= granted)print(granted <= required)print(required.isdisjoint({"admin"}))Example 4 · Checking permissions
required = {"read", "write", "delete"}granted = {"read", "write"}print(required <= granted)print(sorted(required - granted))print(granted.isdisjoint({"admin"}))False ['delete'] True
What this code does
required <= grantedis False becausedeleteis not granted.required - grantedlists exactly what is missing.isdisjointis True becausegrantedhas noadmin.
Both required permissions are inside granted, so the first test is True. The reverse is False, and required has nothing in common with {"admin"}, so the last is True.
Next step
Check your understanding
9 quick questions