Dictionaries & Sets · Question 11 · Medium

Difference and symmetric difference of two sets are printed in sorted form. What is the output?

a = {1, 2, 3, 4}
b = {3, 4, 5}
print(sorted(a - b), sorted(a ^ b))
  1. [1, 2] [3, 4]
  2. [5] [1, 2, 5]
  3. [1, 2] [1, 2, 5]
  4. [1, 2, 5] [1, 2]
Answer and explanation

Answer: C. [1, 2] [1, 2, 5]

Explanation: a - b keeps the elements of a that are not in b: 1 and 2. a ^ b keeps the elements that are in exactly one of the two sets: 1, 2 and 5.

Previous: A list is used as a dictionary key. What happens when this runs? · Next: A dictionary comprehension maps numbers to their squares. What is printed?

Practise dictionaries & sets

Try a fresh set of dictionaries & sets questions with an explanation after every answer, free and without signing up: Dictionaries & Sets practice. Or sit a full placement mock test.

More dictionaries & sets questions

More python practice

Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge