Built-ins & Standard Library · Question 2 · Easy

sorted is called with reverse=True on a list. What does the code print for the result and for the original list?

nums = [3, 1, 2]
print(sorted(nums, reverse=True), nums)
  1. [3, 2, 1] [3, 2, 1]
  2. [1, 2, 3] [3, 1, 2]
  3. None [3, 2, 1]
  4. [3, 2, 1] [3, 1, 2]
Answer and explanation

Answer: D. [3, 2, 1] [3, 1, 2]

Explanation: sorted returns a new list and leaves the original untouched; reverse=True gives descending order. It is the list method nums.sort() that changes the list in place and returns None.

Previous: sum, len, max and min are applied to a list of three marks. What two numbers… · Next: any and all are each given a list containing a mix of truthy and falsy values…

Practise built-ins & standard library

Try a fresh set of built-ins & standard library questions with an explanation after every answer, free and without signing up: Built-ins & Standard Library practice. Or sit a full placement mock test.

More built-ins & standard library questions

More python practice

Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects

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