Sets
Overview
Teaching: 10 min
Exercises: 10 minQuestions
What is a set, and how do I use it?
Objectives
Explain how sets work.
Learn about set operations.
A set keeps an unordered collection of unique items.
- Different from lists
- Lists have a defined order and can contain duplicate values, whereas sets are unordered and contain only unique items.
- Efficient operations
- Sets offer fast membership testing and support operations like union, intersection, and difference.
- Mutability
- You can add or remove items from a set, but duplicate entries are ignored.
beatles = set(['John', 'Paul', 'George', 'Ringo'])
print('Beatles:', beatles)
print('Length:', len(beatles))
beatles.add('Ringo') # Adding 'Ringo' again does nothing
print('Beatles:', beatles)
print('Length:', len(beatles))
Beatles: {'John', 'Paul', 'George', 'Ringo'}
Length: 4
Beatles: {'John', 'Paul', 'George', 'Ringo'}
Length: 4
Check Membership with in
Use the in keyword to test if an element exists in a set.
print('Ringo is one of the Beatles:', 'Ringo' in beatles)
print('Keith is one of the Beatles:', 'Keith' in beatles)
Ringo is one of the Beatles: True
Keith is one of the Beatles: False
Adding and Removing Items
Add Items with add()
beatles.add('Pete')
print('After adding Pete:', beatles)
After adding Pete: {'John', 'Paul', 'George', 'Ringo', 'Pete'}
Remove Items with remove()
beatles.remove('Pete')
print('After removing Pete:', beatles)
After removing Pete: {'John', 'Paul', 'George', 'Ringo'}
Set Operations
Union of Sets
Combine two sets using union() (or the | operator):
odd = set([1, 3, 5, 7, 9])
even = set([2, 4, 6, 8, 10])
all_numbers = odd.union(even)
print('Odd numbers:', odd)
print('Even numbers:', even)
print('All numbers:', all_numbers)
Odd numbers: {1, 3, 5, 7, 9}
Even numbers: {2, 4, 6, 8, 10}
All numbers: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Note
Since sets are unordered, the printed order of elements may vary.
Intersection of Sets
Retrieve common elements with intersection():
primes = set([2, 3, 5, 7])
odd_primes = primes.intersection(odd)
print('Primes that are odd:', odd_primes)
Primes that are odd: {3, 5, 7}
Difference of Sets
Get elements in one set but not in another with difference():
even_non_primes = even.difference(primes)
print('Even numbers that are not prime:', even_non_primes)
Even numbers that are not prime: {4, 6, 8, 10}
Order Matters in
difference()The result of
difference()depends on the order of the sets.
Converting a Set to a Sorted List
To display a set in order, convert it to a list and sort it:
sorted_primes = sorted(primes)
print('Sorted primes:', sorted_primes)
Sorted primes: [2, 3, 5, 7]
Initialising Challenge
What does the following program print?
letters = set('Hello world!') sorted_letters = list(letters) sorted_letters.sort() print('Letters in greeting:', sorted_letters)
Fill in the Blanks Challenge
Fill in the blanks “__” so that the program below produces the output shown.
multiples_of_two = set([2, 4, 6, 8, 10]) multiples_of_three = set([3, 6, 9]) result1 = multiples_of_two.__(multiples_of_three) print('1', result1) result2 = multiples_of_three.__(multiples_of_two) sorted_result2 = sorted(result2) print('2', sorted_result2)1 {6} 2 [3, 9]
Comparing Bacterial Isolates
You have two sets representing bacteria isolated from two different sources:
clinical_isolates = {"Staphylococcus aureus", "Escherichia coli", "Pseudomonas aeruginosa", "Klebsiella pneumoniae"} environmental_isolates = {"Bacillus subtilis", "Escherichia coli", "Staphylococcus epidermidis", "Pseudomonas aeruginosa"}Write code to:
- Print the bacteria common to both sets (intersection).
- Print the bacteria unique to the clinical sample (difference).
- Print all unique bacteria from both sets (union) as a sorted list.
Solution
~~~python # 1. Intersection: bacteria present in both samples common_bacteria = clinical_isolates.intersection(environmental_isolates) print("Common bacteria:", common_bacteria) # 2. Difference: bacteria unique to the clinical sample unique_clinical = clinical_isolates.difference(environmental_isolates) print("Unique to clinical sample:", unique_clinical) # 3. Union: all unique bacteria from both sets, sorted alphabetically all_bacteria = sorted(clinical_isolates.union(environmental_isolates)) print("All unique bacteria:", all_bacteria) ~~~
Key Points
A set stores an unordered collection of unique values.
Sets automatically remove duplicate entries.
Sets support operations like union, intersection, and difference.