Union and intersection in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we find the union or intersection of two sets, we want to know how the time needed changes as the sets get bigger.
We ask: How does the work grow when the input sets grow?
Analyze the time complexity of the following code snippet.
def union_and_intersection(set1, set2):
union = set1 | set2
intersection = set1 & set2
return union, intersection
This code finds the union and intersection of two sets using built-in operators.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Combining elements from both sets to form union and intersection.
- How many times: Each element in both sets is checked once during these operations.
As the size of the sets grows, the time to find union and intersection grows roughly in proportion to the total number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 checks |
| 100 | About 200 checks |
| 1000 | About 2000 checks |
Pattern observation: Doubling the input roughly doubles the work needed.
Time Complexity: O(n + m)
This means the time grows linearly with the size of both input sets combined.
[X] Wrong: "Union and intersection take constant time because sets are fast."
[OK] Correct: Even though sets are fast, the operations still need to look at each element at least once, so time grows with input size.
Understanding how set operations scale helps you explain your code clearly and shows you know how data size affects performance.
"What if we used lists instead of sets for union and intersection? How would the time complexity change?"
Practice
union operation do when applied to two sets in Python?Solution
Step 1: Understand union operation
The union of two sets includes every unique element from both sets without duplicates.Step 2: Compare with other options
Options B and C describe subsets, and D describes intersection, not union.Final Answer:
Combines all unique elements from both sets -> Option AQuick Check:
Union = all unique elements combined [OK]
- Confusing union with intersection
- Thinking union only takes elements from one set
- Assuming union keeps duplicates
a and b in Python?Solution
Step 1: Recall intersection syntax
In Python, the intersection of two sets is found using the&operator or the.intersection()method.Step 2: Check other operators
|is union,+is invalid for sets,-is difference.Final Answer:
a & b -> Option CQuick Check:
Intersection uses & operator [OK]
- Using | instead of & for intersection
- Trying to add sets with + operator
- Confusing difference (-) with intersection
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1.intersection(set2))Solution
Step 1: Identify intersection elements
The intersection contains elements present in both sets: {3, 4}.Step 2: Confirm output
Printing the intersection returns {3, 4} exactly.Final Answer:
{3, 4} -> Option DQuick Check:
Common elements = {3, 4} [OK]
- Confusing union with intersection output
- Expecting all elements combined
- Mixing up set contents
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 + set2)Solution
Step 1: Identify the error cause
Python sets do not support the+operator; this causes a TypeError.Step 2: Correct way to union sets
Use|operator or.union()method to combine sets.Final Answer:
Sets cannot be added with + operator -> Option AQuick Check:
+ operator invalid for sets [OK]
- Trying to add sets with +
- Ignoring error message
- Confusing list addition with set union
list1 = ['Anna', 'Bob', 'Cara'] and list2 = ['Bob', 'Diana', 'Anna'], which Python code correctly finds students present in both lists using set operations?Solution
Step 1: Convert lists to sets for set operations
Lists must be converted to sets to use intersection (&) operator.Step 2: Use & operator to find common elements
set(list1) & set(list2)returns elements in both sets.Step 3: Check other options
list1 | list2 uses | on lists (invalid), C uses + on sets (invalid), D calls intersection on list (no such method).Final Answer:
set(list1) & set(list2) -> Option BQuick Check:
Convert lists to sets, then & for intersection [OK]
- Using + or | on lists directly
- Calling set methods on lists
- Not converting lists to sets first
