Collections with no duplicates and no fixed order — built for membership tests, duplicate removal and set algebra.
01
Learning objectives
Create a set and explain why duplicate values disappear automatically.
Use the in operator to test membership of a set.
Compute union, intersection and difference with |, & and -.
Convert a list to a set to remove duplicates, and back to an ordered list with sorted().
Explain why sets are unordered and cannot be indexed.
02
Lesson notes
A set is a bag of values with two strict rules: every value appears at most once, and the values have no position. Write a = {1, 2, 3, 4}; feed it duplicates and they vanish, because {1, 2, 2, 3, 3, 3} is exactly {1, 2, 3}. Because there is no position, indexing like a[0] is impossible and Python raises TypeError. When you need the values back in a definite order, wrap them: sorted(a) hands you a sorted list.
What sets lose in order they repay in speed: a membership test, 4 in a, is answered almost instantly even when the set holds a million values, because Python hashes each value and jumps to its slot. The same test on a long list may crawl through every element. So the design rule is: use a list when position matters, use a set when the only question is whether a value is present — attendance registers, enrolled roll numbers, visited web pages.
Sets model the maths you already know. Union a | b is every value in either set; intersection a & b is only the shared values; difference a - b is what is in a but missing from b. A concrete example: physics students union maths students gives the whole science club; the intersection gives students taking both; the difference gives physics-only students. Two or three exam questions every year are exactly this calculation in disguise.
Converting is one call: set(rolls) turns a list into a set and destroys duplicates — the fastest duplicate-removal trick in Python. Going back, sorted(unique) returns an ordered list. One syntax trap: an empty set is set(), not {} — empty braces create an empty dictionary, a distinction the examiner checks deliberately. Grow a set with a.add(5) and shrink it with a.remove(5).
Board tip: when a question says remove duplicates and show in ascending order, the expected answer is two lines plus a sentence: unique = set(data), then print(sorted(unique)), then one line noting that a set is unordered, so sorted() is needed for display. Stating both the property (unordered) and the remedy (sorted) is what separates a full-mark answer from a half-mark one — rehearse the three pieces as a fixed ritual.
03
Worked code examples
Duplicate removal and set algebra
Duplicates collapse immediately, so the literal holds 3 values. The operators |, & and - compute union, intersection and difference; sorted() gives the results a display order.
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(len({1, 2, 2, 3, 3, 3}))
print(sorted(a | b))
print(sorted(a & b))
print(sorted(a - b))
print(4 in a, 9 in a)
Output
3
[1, 2, 3, 4, 5]
[3, 4]
[1, 2]
True False
Cleaning a list of roll numbers
set() keeps one copy of each value, shrinking 6 entries to 4; sorted() then returns the ordered list for printing.