(a) Write a Python expression to check if the key, 'RNo' is present in D1.
OR
(b) Write a Python expression to check if any key in D1 has a value 12.
(a) Write a Python expression to check if the key, 'RNo' is present in D1.
OR
(b) Write a Python expression to check if any key in D1 has a value 12.
Both parts test the in membership operator on a dictionary — once on keys (default) and once on values.
(a) Check if key 'RNo' is present in D1:
``python
'RNo' in D1
`
This returns True if 'RNo' is a key of D1, else False.
(b) Check if any key has value 12:
`python
12 in D1.values()
`
Since in on a dictionary by default searches only keys, values must be checked explicitly using D1.values()`.
Marking Scheme
- 11 mark: correct expression `'RNo' in D1` for part (a), OR correct expression `12 in D1.values()` for part (b); any logically equivalent syntactically correct alternative is accepted.
Hint
Remember: `in` on a dictionary checks keys by default; use `.values()` to check values.
Quick Oral Answer
The `in` operator checks dictionary keys by default in O(1) time via hashing; to check values I must explicitly call `.values()`, which is an O(n) linear search.
Analysis & Explanation
This question checks whether a student understands that a Python dictionary is a key-value hash map and that the in operator behaves differently depending on what part of the dictionary is being searched.
Concept
key in D1searches only the keys, using Python's underlying hash table, so it runs in O(1) average time.- To search values,
D1.values()must be called explicitly to get a view of all values, and theninperforms a linear O(n) scan over that view.
Exam trap
Students often write 12 in D1 for part (b), which actually checks whether 12 is a key, not a value — this is the single most common error examiners report for this question type.
Real-world relevance
This pattern is used constantly in real programs, e.g. checking if a roll number/ID already exists before inserting a new record, or checking if any student scored a particular mark.
Common Mistakes
- 1Writing `12 in D1` instead of `12 in D1.values()` for part (b) — this silently checks keys instead of values and gives a wrong logical result without raising an error.
- 2Confusing `D1.keys()` and `D1.values()` — since both return view objects, students sometimes swap them.
Interesting Facts
Key lookup in a Python dictionary using `in` runs in average O(1) time because dictionaries are implemented as hash tables internally.
Value lookup using `.values()` is O(n) because there is no hash index built on values — Python must scan every entry linearly.
Spotted a mistake or something unclear?
Tell us — we fix reported answers fast.
Frequently Asked Questions
Does 'in' check keys or values by default in a Python dictionary?
By default, the `in` operator on a dictionary checks only the keys, not the values. To check values you must explicitly use `D1.values()`.
What is the time complexity of key lookup vs value lookup in a dictionary?
Key lookup via `in` is O(1) on average due to hashing, while value lookup via `D1.values()` is O(n) since it requires a linear scan of all values.