This question challenges you to consider how Python handles list and set operations.
You’ll need to understand how sets work to remove duplicates and how sorting a collection transforms its order.
Let’s see how well you grasp these concepts!
Contents
Question: Python’s List and Set Operations
Given the following Python code, what will be the output and why?
numbers = [4, 6, 2, 8, 6, 2, 9, 1]
unique_sorted_numbers = sorted(set(numbers))
print(unique_sorted_numbers)
Answer: B
Explanation:
- Converting
numbers
to a set removes duplicates, resulting in{1, 2, 4, 6, 8, 9}
(note that sets are unordered collections of unique elements). - Sorting this set gives the list
[1, 2, 4, 6, 8, 9]
.