Q26
1 markShort AnswerSection B

(a) Write a single statement using a BUILT_IN function to add the key : value pair 'RNo' : 12, if the key 'RNo' is not present in D1. However, if the key 'RNo' is present, the function should return its value.

OR

(b) Write a single statement to delete all the elements from D1.

Python Dictionaries
Dictionary Built-in Methods
Official Answer

Both parts require knowledge of specific built-in dictionary methods.


(a) Add key 'RNo':12 only if absent, else return existing value:

``python

D1.setdefault('RNo', 12)

`

setdefault() is the exact built-in function designed for this behaviour — insert-if-absent, return-if-present.


(b) Delete all elements from D1:

`python

D1.clear()

`

clear() empties the dictionary in place, leaving D1 as {}`.

setdefault()clear()dictionary methodsbuilt-in functionPython dictionaryin-place operation

Marking Scheme

  • 11 mark: correct statement `D1.setdefault('RNo', 12)` for part (a), OR correct statement `D1.clear()` for part (b).

Hint

Use `setdefault()` for insert-if-absent-else-return, and `clear()` to empty a dictionary in place.

Quick Oral Answer

setdefault() inserts a key with a default value only if it is missing and returns the existing value otherwise; clear() empties all elements from the dictionary while keeping the same object.

Analysis & Explanation

This question tests recall of two specific, commonly used dictionary built-in methods.


Concept

  • setdefault(key, default) is a single method call that combines a conditional check and an insertion — it avoids writing an explicit if key not in D1: block.
  • clear() removes all key-value pairs but keeps the dictionary object itself (its id() is unchanged), unlike D1 = {} which creates a brand-new empty dictionary object.

Exam trap

A very common wrong answer for part (a) is writing an if-else block instead of the required 'single statement using a BUILT-IN function' — the question explicitly demands setdefault().

For part (b), students sometimes write del D1, which deletes the entire dictionary variable rather than just emptying it — a critical difference in behaviour.


Real-world relevance

setdefault() is widely used for counting/grouping tasks, e.g. initializing a counter for a word only the first time it is seen.

Common Mistakes

  1. 1Writing an if-else block for part (a) instead of using the required built-in `setdefault()` function, losing marks for not following the instruction.
  2. 2Using `del D1` instead of `D1.clear()` for part (b) — `del D1` removes the variable entirely, while `clear()` empties it but keeps it usable.

Interesting Facts

`setdefault()` was added to Python's dictionary type specifically to avoid the two-step 'check-then-insert' idiom, making code both shorter and slightly faster.

`D1.clear()` runs in O(n) time as it must deallocate every key-value pair, but the dictionary object's memory allocation for buckets may be retained internally by CPython for efficiency.

Spotted a mistake or something unclear?

Tell us — we fix reported answers fast.

Frequently Asked Questions

What does D1.setdefault('RNo', 12) do if 'RNo' already exists?

If 'RNo' already exists in D1, setdefault() does not modify the dictionary and simply returns the existing value associated with 'RNo'.

Is there a difference between D1.clear() and D1 = {}?

Yes. D1.clear() empties the existing dictionary object in place (same id()), while D1 = {} creates a brand new empty dictionary object and rebinds the name D1 to it.