Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

Which of the following will delete key-value pair for key = “Red” from a dictionary D1?

a. delete D1("Red")

b. del D1["Red"]

c. del.D1["Red"]

d. D1.del["Red"]


Answer by student:

b. del D1[“Red”]

Detailed answer by teachoo:

To answer this question, we need to know the syntax and usage of dictionaries and the del statement in Python.

  • A dictionary is a data structure that stores key-value pairs, where each key is associated with a value. A dictionary is created by enclosing the key-value pairs in curly braces {} and separating them by commas. For example, D1 = {“Red”: 1, “Green”: 2, “Blue”: 3} is a dictionary that maps colors to numbers.
  • The del statement is used to delete an item from a dictionary or a list. The syntax of the del statement is:

del dictionary_name[key]

  • where dictionary_name is the name of the dictionary and key is the key of the item to be deleted. For example, del D1[“Red”] will delete the item with key “Red” from the dictionary D1.

In this question, we have a dictionary D1 and we want to delete the key-value pair for key = “Red” from it. We need to find the correct option that will do this.

Let’s go through each of the options and see why they are correct or incorrect:

  • a. delete D1(“Red”): This is incorrect because there is no delete keyword in Python. Also, the parentheses () are not used to access items in a dictionary, but the square brackets [] are.
  • b. del D1[“Red”]: This is correct because it follows the syntax and usage of the del statement and dictionaries in Python. It will delete the item with key “Red” from the dictionary D1.
  • c. del.D1[“Red”]: This is incorrect because there should be no dot (.) between del and D1. The dot (.) is used to access attributes or methods of an object, but del is not an object, but a statement.
  • d. D1.del[“Red”]: This is incorrect because there is no del attribute or method for dictionaries in Python. Also, the dot (.) is not used to delete items from a dictionary, but the del statement is.

So, the correct answer is b. del D1[“Red”].

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.