If D is a Python dictionary as D={'A': 1, 'B': 2, 'C: 3} Then, which of the…

2023

If D is a Python dictionary as D={'A': 1, 'B': 2, 'C: 3} Then, which of the following command will remove the entire dictionary from the memory?

  1. A.

    del(D)

  2. B.

    D.del()

  3. C.

    D.clear()

  4. D.

    D.remove()

Attempted by 2119 students.

Show answer & explanation

Correct answer: A

Correct command: del(D) or del D

Explanation: del(D) removes the name D from the current namespace. If there are no other references to the dictionary object, it becomes unreachable and Python's garbage collector will reclaim its memory. After deleting the name, attempting to access D will raise a NameError.

  • D.clear() removes all items from the dictionary but does not delete the variable or the dictionary object; D remains defined as an empty dict ({}).

  • D.del() is not a valid method on dict objects and will raise an AttributeError.

  • D.remove() is not a dict method (remove() applies to lists); calling it on a dict will raise an AttributeError.

Example: after executing del D, referencing D will cause NameError: name 'D' is not defined.

Explore the full course: Rssb Senior Computer Instructor