Quick Answer
Perform mathematical set operations in Python.
Understanding the Issue
Sets store unique elements and support mathematical set operations like union, intersection, difference, and symmetric difference. Useful for removing duplicates, membership testing, and comparative operations between collections.
The Problem
This code demonstrates the issue:
Python
Error
A = {1, 2, 3}
B = {3, 4, 5}
# Need common elements and unique elements
The Solution
Here's the corrected code:
Python
Fixed
# Intersection (common elements)
common = A & B # {3}
# Union (all unique elements)
all_items = A | B # {1,2,3,4,5}
# Remove duplicates from list
unique = list(set([1,2,2,3])) # [1,2,3]
Key Takeaways
Use sets for efficient uniqueness operations.