Skip to content
Question 30 of 97

Q.Consider the following dictionaries, D and D1 : D={"Suman": 40, "Raj":55, "Raman":60} D1={"Aditi":30, "Amit":90,"Raj":20} (Answer using built-in Python functions only)

(i)
(a) Write a statement to display/return the value corresponding to the key "Raj" in the dictionary D. OR
(b) Write a statement to display the length of the dictionary D1.
(ii)
(a) Write a statement to append all the key-value pairs of the dictionary D to the dictionary D1. OR
(b) Write a statement to delete the item with the given key "Amit" from the dictionary D1.
Sikkim CbseCBSE Class XII Board 2025Subjective· 2mImportance★★★★★
31% · 30/97 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Python dictionary operations: access values with D[key], find length with len(), merge with .update(), and delete with del or .pop(). The solutions use built-in methods to manipulate the given dictionaries.

Dictionaries in Python store data as key-value pairs, where each unique key maps to a value. The question tests fundamental dictionary operations that you'll use constantly in programming and data manipulation.

The key insight is that Python provides direct, readable syntax for these operations. You don't need loops or complex logic—the language gives you built-in functions and methods designed specifically for dictionary manipulation.

Let me work through each part:

(i) Accessing and Measuring Dictionaries

(a) Display the value for key "Raj" in D

To retrieve a value from a dictionary, you use square-bracket notation with the key:

print(D["Raj"])

This accesses the value associated with "Raj" in dictionary D, which is 55. The print() function displays it.

Alternatively, you could use the .get() method:

print(D.get("Raj"))
Tip

Use .get(key) instead of [key] when you're unsure if the key exists—it returns None instead of raising a KeyError.

(b) Display the length of D1

The len() function counts the number of key-value pairs:

print(len(D1))

Dictionary D1 has three entries: "Aditi", "Amit", and "Raj", so this returns 3.

(ii) Modifying Dictionaries

(a) Append all key-value pairs from D to D1

The .update() method merges one dictionary into another:

D1.update(D)

This adds all entries from D into D1. If a key already exists in D1 (like "Raj"), its value gets overwritten by the value from D.

After this operation:

D1 = {"Aditi": 30, "Amit": 90, "Raj": 60, "Suman": 40, "Raman": 60}
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.