Edexcel International Advanced Level Computer Science, point 8.2.7
A set (4.2.1) is an unordered collection of unique items. You must be able to select and use methods to handle a set in code. (Check the PLS for the exact operations allowed.)
Creating a set
a = {1, 2, 3, 4} # a set literal
# from a list: duplicates removed -> {3, 4, 5}
b = set([3, 4, 5, 5])
# an EMPTY set (not {}, which is a dictionary)
empty = set()
letters = set("hello") # {'h', 'e', 'l', 'o'}
- No duplicates, and no order (so no index:
a[0]is aTypeError). - Items must be immutable (numbers, strings, tuples), not lists.
a. Membership
Is an item in the set? This is fast, even for large sets.
print(3 in a) # True
print(9 in a) # False
print(9 not in a) # True
b. Union
All items in either set (no duplicates). Mathematically .
print(a | b) # {1, 2, 3, 4, 5}
print(a.union(b)) # same
c. Intersection
Items in both sets. .
print(a & b) # {3, 4}
print(a.intersection(b)) # same
d. Difference
Items in the first set but not the second. . The order matters.
print(a - b) # {1, 2}
print(b - a) # {5}
print(a.difference(b)) # {1, 2}
Summary
With A = {1, 2, 3, 4} and B = {3, 4, 5}:
| Operation | Python | Result |
|---|---|---|
| Union | A | B |
{1, 2, 3, 4, 5} |
| Intersection | A & B |
{3, 4} |
| Difference | A - B |
{1, 2} |
| Difference | B - A |
{5} |
| Operation | Symbol | Maths | Result |
|---|---|---|---|
| Membership | in |
True/False | |
| Union | | |
All items in either | |
| Intersection | & |
Items in both | |
| Difference | - |
In A, not B |
Adding and removing
a.add(10) # add an item
# remove if present (no error if missing)
a.discard(99)
a.remove(10) # remove (KeyError if missing)
print(len(a)) # number of items
for x in a: # traverse (order not guaranteed)
print(x)
Uses of sets
- Removing duplicates:
unique = set(my_list). - Checking membership quickly: a set of banned words.
- Finding items common to two groups (intersection), or in one but not the other (difference).
Worked example
Task: two clubs have members chess = {"Amy", "Raj", "Sam", "Li"} and drama = {"Raj", "Zoe", "Li", "Max"}. Find (a) those in both clubs, (b) those in at least one, (c) those in chess only, and (d) whether "Zoe" is in chess.
chess = {"Amy", "Raj", "Sam", "Li"}
drama = {"Raj", "Zoe", "Li", "Max"}
both = chess & drama # (a)
either = chess | drama # (b)
chess_only = chess - drama # (c)
print(both) # {'Raj', 'Li'}
print(len(either)) # 6
print(chess_only) # {'Amy', 'Sam'}
print("Zoe" in chess) # False (d)
- (a) Intersection: Raj and Li.
- (b) Union: Amy, Raj, Sam, Li, Zoe, Max, which is 6 people ("Raj" and "Li" are counted once).
- (c) Difference: Amy and Sam.
- (d) Membership:
False.
(The order the items print in may differ, because a set has no order.)
Exam tip: be clear about which operation is which (| union, & intersection, - difference), and that difference depends on the order. Remember an empty set is set(), not {}. Sets have no duplicates and no index.
Check yourself
What is {1, 2, 3} | {3, 4}?
{1, 2, 3, 4}.
What is {1, 2, 3} - {3, 4}?
{1, 2}.
How do you create an empty set in Python?
set(). Using {} creates an empty dictionary.
Why can't you index a set?
A set is unordered, so its items have no positions.