PCEP-30-02 · Question #51
Consider the following list. data = [1, 5, 10, 19, 55, 30, 99] Which of the code snippets below would produce a new list like the following? [1, 5, 10, 99]
The correct answer is D. data.pop(5) data.remove(19) data.remove(55). Option D works because it correctly combines pop() and remove() - two methods that operate differently: pop(index) removes an element by its position, while remove(value) removes an element by its value. Calling data.pop(5) removes the element at index 5 (which is 30), leaving…
Question
Options
- Adata.pop(5) data.pop(19) data.pop(55)
- Bdata.pop(1) data.pop(3) data.pop(4) data.pop(6)
- CNone of the above.
- Ddata.pop(5) data.remove(19) data.remove(55)
- Edata.remove(5) data.remove(19) data.remove(55)
How the community answered
(46 responses)- A4% (2)
- B2% (1)
- C9% (4)
- D83% (38)
- E2% (1)
Explanation
Option D works because it correctly combines pop() and remove() - two methods that operate differently: pop(index) removes an element by its position, while remove(value) removes an element by its value. Calling data.pop(5) removes the element at index 5 (which is 30), leaving [1, 5, 10, 19, 55, 99], then data.remove(19) and data.remove(55) remove those values by value, yielding [1, 5, 10, 99].
Why the distractors fail:
- A passes values (
19,55) topop(), which expects indices -data.pop(19)raises anIndexErrorsince the list only has 7 elements. - B pops index
1(which removes5, a value we need to keep), anddata.pop(6)raises anIndexErrorafter the list has already shrunk. - C is wrong because D is valid.
- E calls
data.remove(5), which removes the value5- something we need to keep - leaving30in the list and producing[1, 10, 30, 99]instead.
Memory tip: Think of it as "pop the position, remove the value" - pop() takes an index number like an array slot, remove() takes the actual element you want gone.
Community Discussion
No community discussion yet for this question.