KnowledgeBoat Logo

Computer Science

List AL is defined as follows : AL = [1, 2, 3, 4, 5]
Which of the following statements removes the middle element 3 from it so that the list AL equals [1, 2, 4, 5] ?

  1. del AL[2]
  2. AL[2:3] = []
  3. AL[2:2] = []
  4. AL[2] = []
  5. AL.remove(3)

Python List Manipulation

1 Like

Answer

del AL[2]
AL[2:3] = []
AL.remove(3)

Reason — del AL[2] — The del keyword deletes the element from the list AL from index 2.
AL[2:3] = [] — The slicing of the list AL[2:3] begins at index 2 and ends at index 2. Therefore, the element at index 2 will be replaced by an empty list [].
AL.remove(3) — The remove() function removes an element from the list AL from index 3."

Answered By

1 Like


Related Questions