Free preview
100 lessons
Python Done Right
Free preview
Python Done Right · 100 lessons
No surprise gaps
Actually remember it
Skip what you know
One subscription. All learning paths included.
Our content is best on a larger screen
Set union
The union of two sets, and , is a set that contains all the elements present in either or without repeating any elements. If an element appears in both sets, it will only appear once in the resulting union set.
In Python, we can use the .union() method or the | operator to perform this operation.
Using the .union() method
development_team = {"Alice", "Bob", "Clara"}
design_team = {"Bob", "Dana", "Ellie"}
# Find the union of the two sets
all_team_members = development_team.union(design_team)
print(all_team_members)
Output:
{'Alice', 'Clara', 'Dana', 'Ellie', 'Bob'}
The union is commutative - A.union(B) == B.union(A) - since we're simply combining all unique elements.
Using the | operator
We can also use the | operator:
frontend_team = {"Eli", "Juan", "Megan"}
backend_team = {"Megan", "Nina", "Paul"}
# Use the | operator to find the union
complete_team = frontend_team | backend_team
print(complete_team)
Output:
{'Paul', 'Eli', 'Nina', 'Megan', 'Juan'}
The | operator is especially readable when combining multiple sets:
development_team = {"Alice", "Bob", "Clara"}
design_team = {"Bob", "Dana", "Ellie"}
product_team = {"Tom", "Alice", "David"}
all_team_members = development_team | design_team | product_team
print(all_team_members)
Output:
{'Dana', 'David', 'Tom', 'Bob', 'Ellie', 'Alice', 'Clara'}
Note that there are no duplicates in the resulting set.
Practice questions
4 questions
In the Python Console, two sets are defined: dog_breeds and cat_breeds which represent a list of breeds found in a specific animal shelter for dogs and cats, respectively.
Combine these sets to find all the unique animal breeds present in the shelter and assign the result to a variable named answer.
Once you have the correct set of breeds, press submit.
+ 3 more questions
Set intersection
The intersection of two sets, and , is a set that contains only the elements that are present in both and , excluding all items that are in only one of the sets.
In Python, we can utilise the .intersection() method or the & operator to perform this operation.
Using the .intersection() method
odd_numbers = {1, 3, 5, 7, 9}
prime_numbers = {2, 3, 5, 7, 11}
# Find the intersection of the two sets
common_numbers = odd_numbers.intersection(prime_numbers)
print(common_numbers) # Output: {3, 5, 7}
Like union, intersection is commutative - A.intersection(B) == B.intersection(A).
Using the & operator
We can also use the & operator:
team_a = {"Lisa", "Tom", "Harry"}
team_b = {"Tom", "Harry", "Sally"}
# Use the & operator to find the intersection
common_members = team_a & team_b
print(common_members) # Output: {'Tom', 'Harry'}
The & operator is especially readable when intersecting multiple sets:
math_students = {"Anna", "John", "Sam"}
science_students = {"Sam", "Lily", "Anna"}
literature_students = {"Anna", "Tom", "Sam"}
honor_students = math_students & science_students & literature_students
print(honor_students) # Output: {'Anna', 'Sam'}
Practice questions
4 questions
You’re organising a technology conference and have three sets of skills available in the Python Console: frontend_skills, backend_skills, and attendee_skills. These represent frontend skills, backend skills, and skills listed by attendees, respectively.
Write code to find the unique skills that attendees have listed which are relevant to both frontend and backend development. Assign the result to a variable named answer, then press submit.
+ 3 more questions
Set difference
The set difference between two sets, and , results in a new set containing all the elements that are present in set but not in set . This operation effectively subtracts set from set , and the elements that are common in both sets are not included in the result.
In Python, we can perform this operation using the .difference() method or the - operator.
Using the .difference() method
primary_colors = {"red", "blue", "yellow"}
secondary_colors = {"green", "orange", "purple", "red"}
# Find the difference of the two sets
unique_primary_colors = primary_colors.difference(secondary_colors)
print(unique_primary_colors) # Output: {'yellow', 'blue'}
Using the - operator
We can also use the - operator:
new_hires = {"Jesse", "Victor", "Julie"}
current_employees = {"Julie", "Michael", "Lucas"}
# Use the - operator to find the difference
exclusive_new_hires = new_hires - current_employees
print(exclusive_new_hires) # Output: {'Victor', 'Jesse'}
Unlike union and intersection, difference is not commutative - A - B gives different results to B - A.
Practice questions
4 questions
You’re coordinating a training program for a software company and have two sets of skills available in the Python Console:
required_skills: the skills required for the program.employee_skills: the skills an employee currently has.Write code to find the skills that the employee still needs to learn to meet the program requirements. Assign the result to a variable named answer, then press submit.
+ 3 more questions