All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Sets: membership, union, intersection and difference

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

A set is an unordered collection with no duplicates. It is ideal for testing membership and for comparing two groups.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Creating sets (spec 8.2.7)

Write a set with braces, or convert another structure with set(). Note that {} on its own is an empty dictionary: use set() for an empty set. Duplicates are removed automatically, and a set has no index, so s[0] is a TypeError.

a = {3, 1, 3, 2}
print(len(a), 2 in a, 5 in a)
b = set((4, 5, 4))
b.add(6)
b.discard(9)
print(len(b))
print(type({}), type(set()))

Output:

3 True False
3
<class 'dict'> <class 'set'>

remove(x) raises KeyError if x is missing; discard(x) does not.

Set operations

OperationOperatorMethodContains
Uniona | ba.union(b)items in either set
Intersectiona & ba.intersection(b)items in both sets
Differencea - ba.difference(b)items in a but not in b
Symmetric differencea ^ ba.symmetric_difference(b)items in exactly one of the sets

Using them

Sets print in an unpredictable order, so sort them for display with sorted().

maths = {"Ana", "Ben", "Cai", "Dev"}
physics = {"Ben", "Dev", "Eve"}
print(sorted(maths | physics))
print(sorted(maths & physics))
print(sorted(maths - physics))
print(sorted(maths ^ physics))

Output:

['Ana', 'Ben', 'Cai', 'Dev', 'Eve']
['Ben', 'Dev']
['Ana', 'Cai']
['Ana', 'Cai', 'Eve']

Worked example

A = {1, 2, 3, 4}, B = {3, 4, 5}. Find A | B, A & B, A - B and B - A.

  1. A | B = [1, 2, 3, 4, 5]
  2. A & B = [3, 4]
  3. A - B = [1, 2]
  4. B - A = [5]

Answer: Difference is not symmetric: A - B and B - A are different

Key idea: Sets hold unique items with no order; in tests membership; | union, & intersection, - difference, ^ symmetric difference; use set() for an empty set.

Check yourself

  1. What is displayed when this code runs? ```python print(len({2, 4, 2, 6, 4})) ```
    Show answer

    3. Duplicates are removed, leaving {2, 4, 6}: 3 items.

  2. What is displayed when this code runs? ```python a = {1, 3, 5, 7} b = {3, 4, 5, 6} print(sorted(a & b)) ```
    Show answer

    [3, 5]. The intersection contains items in both sets: [3, 5].

  3. What is displayed when this code runs? ```python a = {1, 3, 5, 7} b = {3, 4, 5, 6} print(sorted(b - a)) ```
    Show answer

    [4, 6]. b - a keeps the items of b that are not in a: [4, 6].

  4. What is displayed when this code runs? ```python a = {"x", "y"} b = {"y", "z", "w"} print(len(a | b)) ```
    Show answer

    4. The union holds every distinct item: w, x, y, z: 4.

  5. What is displayed when this code runs? ```python vowels = set("aeiou") print("e" in vowels, "y" in vowels) ```
    • True True
    • False True
    • False False
    • True False
    Show answer

    True False. in tests membership: True False.

  6. Which exception is raised when this code runs? ```python s = {1, 2, 3} s.remove(4) ```
    • IndexError
    • KeyError
    • ValueError
    • TypeError
    Show answer

    KeyError. remove() on a missing item raises KeyError; discard() would not.

  7. Which line creates an empty set?
    • s = {}
    • s = []
    • s = ()
    • s = set()
    Show answer

    s = set(). {} is an empty dictionary, [] a list and () a tuple.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.