Bird
Raised Fist0
Pythonprogramming~20 mins

Union and intersection in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
๐ŸŽ–๏ธ
Master of Union and Intersection
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this union operation?
Consider two sets A and B. What will be printed after the union operation?
Python
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B)
A{1, 2, 3}
B{3}
C{1, 2, 4, 5}
D{1, 2, 3, 4, 5}
Attempts:
2 left
๐Ÿ’ก Hint
Union combines all unique elements from both sets.
โ“ Predict Output
intermediate
2:00remaining
What is the output of this intersection operation?
Given two sets X and Y, what will be printed after the intersection operation?
Python
X = {10, 20, 30, 40}
Y = {30, 40, 50, 60}
print(X & Y)
A{30, 40}
B{10, 20}
C{50, 60}
D{10, 20, 30, 40, 50, 60}
Attempts:
2 left
๐Ÿ’ก Hint
Intersection returns only elements common to both sets.
๐Ÿง  Conceptual
advanced
2:00remaining
Which option shows the correct way to find the union of two sets using a method?
You want to find the union of sets set1 and set2 using a set method. Which option is correct?
Aset1.intersection(set2)
Bset1.add(set2)
Cset1.union(set2)
Dset1.update(set2)
Attempts:
2 left
๐Ÿ’ก Hint
Union method returns a new set with all unique elements from both sets.
โ“ Predict Output
advanced
2:00remaining
What is the output of this combined union and intersection code?
Given sets a and b, what will be printed?
Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
result = (a | b) & a
print(result)
A{3, 4}
B{1, 2, 3, 4}
C{5, 6}
D{1, 2}
Attempts:
2 left
๐Ÿ’ก Hint
First union all elements, then intersect with set a.
๐Ÿ”ง Debug
expert
2:00remaining
What error does this code raise?
Examine the code below. What error will it raise when run?
Python
set_a = {1, 2, 3}
set_b = [3, 4, 5]
result = set_a | set_b
print(result)
ATypeError
BSyntaxError
CNameError
DNo error, prints {1, 2, 3, 4, 5}
Attempts:
2 left
๐Ÿ’ก Hint
The union operator requires both operands to be sets.

Practice

(1/5)
1. What does the union operation do when applied to two sets in Python?
easy
A. Combines all unique elements from both sets
B. Finds elements only in the first set
C. Finds elements only in the second set
D. Finds elements common to both sets

Solution

  1. Step 1: Understand union operation

    The union of two sets includes every unique element from both sets without duplicates.
  2. Step 2: Compare with other options

    Options B and C describe subsets, and D describes intersection, not union.
  3. Final Answer:

    Combines all unique elements from both sets -> Option A
  4. Quick Check:

    Union = all unique elements combined [OK]
Hint: Union joins all unique items from both sets [OK]
Common Mistakes:
  • Confusing union with intersection
  • Thinking union only takes elements from one set
  • Assuming union keeps duplicates
2. Which of the following is the correct syntax to find the intersection of two sets a and b in Python?
easy
A. a + b
B. a | b
C. a & b
D. a - b

Solution

  1. Step 1: Recall intersection syntax

    In Python, the intersection of two sets is found using the & operator or the .intersection() method.
  2. Step 2: Check other operators

    | is union, + is invalid for sets, - is difference.
  3. Final Answer:

    a & b -> Option C
  4. Quick Check:

    Intersection uses & operator [OK]
Hint: Use & for intersection of sets [OK]
Common Mistakes:
  • Using | instead of & for intersection
  • Trying to add sets with + operator
  • Confusing difference (-) with intersection
3. What is the output of the following code?
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print(set1.intersection(set2))
medium
A. {1, 2}
B. {5, 6}
C. {1, 2, 3, 4, 5, 6}
D. {3, 4}

Solution

  1. Step 1: Identify intersection elements

    The intersection contains elements present in both sets: {3, 4}.
  2. Step 2: Confirm output

    Printing the intersection returns {3, 4} exactly.
  3. Final Answer:

    {3, 4} -> Option D
  4. Quick Check:

    Common elements = {3, 4} [OK]
Hint: Intersection shows only common elements [OK]
Common Mistakes:
  • Confusing union with intersection output
  • Expecting all elements combined
  • Mixing up set contents
4. The following code is intended to print the union of two sets, but it causes an error. What is the problem?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 + set2)
medium
A. Sets cannot be added with + operator
B. The sets are empty
C. The print statement is missing parentheses
D. The sets have overlapping elements

Solution

  1. Step 1: Identify the error cause

    Python sets do not support the + operator; this causes a TypeError.
  2. Step 2: Correct way to union sets

    Use | operator or .union() method to combine sets.
  3. Final Answer:

    Sets cannot be added with + operator -> Option A
  4. Quick Check:

    + operator invalid for sets [OK]
Hint: Use | or .union(), not + for sets [OK]
Common Mistakes:
  • Trying to add sets with +
  • Ignoring error message
  • Confusing list addition with set union
5. Given two lists of student names, list1 = ['Anna', 'Bob', 'Cara'] and list2 = ['Bob', 'Diana', 'Anna'], which Python code correctly finds students present in both lists using set operations?
hard
A. list1 | list2
B. set(list1) & set(list2)
C. set(list1) + set(list2)
D. list1.intersection(list2)

Solution

  1. Step 1: Convert lists to sets for set operations

    Lists must be converted to sets to use intersection (&) operator.
  2. Step 2: Use & operator to find common elements

    set(list1) & set(list2) returns elements in both sets.
  3. Step 3: Check other options

    list1 | list2 uses | on lists (invalid), C uses + on sets (invalid), D calls intersection on list (no such method).
  4. Final Answer:

    set(list1) & set(list2) -> Option B
  5. Quick Check:

    Convert lists to sets, then & for intersection [OK]
Hint: Convert lists to sets before intersection [OK]
Common Mistakes:
  • Using + or | on lists directly
  • Calling set methods on lists
  • Not converting lists to sets first