PCEP-30-02 · Question #28
Which function does in-place reversal of objects in a list?
The correct answer is D. list.reverse(). list.reverse() performs an in-place reversal of all elements in a list, modifying the original list directly and returning None - exactly what "in-place reversal of objects" describes. The distractors each do something different: A. list.sort(func) - sorts the list (optionally…
Question
Options
- Alist.sort(func)
- Blist.pop(obj=list[-1])
- Clist.remove(obj)
- Dlist.reverse()
How the community answered
(22 responses)- A14% (3)
- B5% (1)
- C5% (1)
- D77% (17)
Explanation
list.reverse() performs an in-place reversal of all elements in a list, modifying the original list directly and returning None - exactly what "in-place reversal of objects" describes.
The distractors each do something different:
- A.
list.sort(func)- sorts the list (optionally with a key function), not reversal - B.
list.pop(obj=list[-1])- removes and returns the last element (or element at a given index); it doesn't accept an object as argument and doesn't reverse anything - C.
list.remove(obj)- removes the first occurrence of a specific value from the list; no reversal involved
Memory tip: Think of reverse() as "reversing the order" - the word itself is your clue. If you need the reversed version without mutating the original, use reversed() (a built-in) or slicing [::-1], but for in-place, list.reverse() is your answer.
Community Discussion
No community discussion yet for this question.