Question 251 markShort AnswerSection B

(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.

Python Dictionaries
Dictionary Membership Operators

Official answer

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()`.

Answer keywords:dictionarymembership operatorinkeysvalues()hashingPython dictionary

Marking scheme

  1. 1.1 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.

Common mistakes

  1. 1.Writing `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.
  2. 2.Confusing `D1.keys()` and `D1.values()` — since both return view objects, students sometimes swap them.

Analysis and 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 D1 searches 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 then in performs 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.

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.

Question details

Q25 of 48 questions

Board
CBSE
Class
Class 12
Subject
Computer Science
Year
2026
Section
Section B
Marks
1
Type
Short Answer
Chapter
Python Dictionaries
Topic
Dictionary Membership Operators
Bloom's level
apply
Syllabus topic
Unit 1: Computational Thinking and Programming – 2 — Python Dictionaries: membership testing with 'in'
Author
CBSE
Reviewed by
Padhantu Academic Team

Spotted a mistake or something unclear?

Tell us and we will fix it.

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.