nerdexam
Python_Institute

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

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]

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)
  • A
    4% (2)
  • B
    2% (1)
  • C
    9% (4)
  • D
    83% (38)
  • E
    2% (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) to pop(), which expects indices - data.pop(19) raises an IndexError since the list only has 7 elements.
  • B pops index 1 (which removes 5, a value we need to keep), and data.pop(6) raises an IndexError after the list has already shrunk.
  • C is wrong because D is valid.
  • E calls data.remove(5), which removes the value 5 - something we need to keep - leaving 30 in 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.

Full PCEP-30-02 Practice