Sets
Express membership and overlap without manual nested searches.
On this page
The concept
A set stores unique hashable values. It has no positional indexing and no promised insertion order. Use a set for questions such as “which values are shared?” and “has this value appeared?” rather than for display order.
Union | combines members, intersection & keeps shared members, difference - keeps members only on the left, and symmetric difference ^ keeps members in exactly one input. Sorting a set for display makes output predictable. An empty set is set(); {} creates an empty dictionary.
Worked example
skills = {"python", "git"}
skills.add("sql")
skills.discard("java")
print(sorted(skills))
a = {1, 2, 3}
b = {3, 4, 5}
print(sorted(a | b))
print(sorted(a & b))
print(sorted(a - b))
print(sorted(a ^ b))
print({1, 2}.issubset(a))Trace the result
- The skills collection gains sql, while discarding absent java is harmless.
- Calculate the union and intersection of a and b by hand before comparing the output.
- issubset asks whether every member of one set is present in the other.
['git', 'python', 'sql']
[1, 2, 3, 4, 5]
[3]
[1, 2]
[1, 2, 4, 5]
TrueLine-by-line walkthrough
Line 1: skills = {"python", "git"}
Evaluate {'python', 'git'}, then bind or update skills with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 2: skills.add("sql")
Call skills.add with the displayed arguments. This call may have an effect even when its return value is not assigned.
Line 3: skills.discard("java")
Call skills.discard with the displayed arguments. This call may have an effect even when its return value is not assigned.
Line 4: print(sorted(skills))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 5: a = {1, 2, 3}
Evaluate {1, 2, 3}, then bind or update a with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 6: b = {3, 4, 5}
Evaluate {3, 4, 5}, then bind or update b with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 7: print(sorted(a | b))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 8: print(sorted(a & b))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 9: print(sorted(a - b))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 10: print(sorted(a ^ b))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 11: print({1, 2}.issubset(a))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Set membership requires hashable members; a list cannot be a set member. Removing duplicates with a set also discards meaningful ordering.