Exercises · Q8
Q.Consider the following dictionary stateCapital:
stateCapital = {"Assam":"Guwahati", "Bihar":"Patna","Maharashtra":"Mumbai", "Rajasthan":"Jaipur"}
Find the output of the following statements:
a) print(stateCapital.get("Bihar"))
b) print(stateCapital.keys())
c) print(stateCapital.values())
d) print(stateCapital.items())
e) print(len(stateCapital))
f) print("Maharashtra" in stateCapital)
g) print(stateCapital.get("Assam"))
h) del stateCapital["Assam"]
print(stateCapital)
print(stateCapital)
Odisha ChseTextbookSubjective· 3mImportance★★★★★est
53% · 16/30 Questions
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 →get(key) returns the value for a key, keys() / values() / items() return view objects
(printed as dict_keys(...), dict_values(...), dict_items(...)), len() counts key-value pairs,
in tests keys only, and del d[key] removes a whole pair. Since Python 3.7 a dictionary
keeps its insertion order, so every view prints in the order the pairs were created.
The dictionary
stateCapital = {"Assam": "Guwahati", "Bihar": "Patna",
"Maharashtra": "Mumbai", "Rajasthan": "Jaipur"}
Statement-by-statement dry run
| # | Statement | Output | Why |
|---|---|---|---|
| a | print(stateCapital.get("Bihar")) | Patna | get() looks the key up and returns its value |
| b | print(stateCapital.keys()) | dict_keys(['Assam', 'Bihar', 'Maharashtra', 'Rajasthan']) | a view of all keys, in insertion order |
| c | print(stateCapital.values()) | dict_values(['Guwahati', 'Patna', 'Mumbai', 'Jaipur']) | a view of all values |
| d | print(stateCapital.items()) | dict_items([('Assam', 'Guwahati'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')]) | a view of (key, value) tuples |
| e | print(len(stateCapital)) | 4 | len() counts key-value pairs, not individual strings |
| f | print("Maharashtra" in stateCapital) | True | in on a dict tests membership among the keys |
| g | print(stateCapital.get("Assam")) | Guwahati | same key lookup as (a) |
| h | del stateCapital["Assam"] then print(stateCapital) | {'Bihar': 'Patna', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'} | del removes the Assam pair; the remaining 3 pairs print in order |
Full expected output
Patna
dict_keys(['Assam', 'Bihar', 'Maharashtra', 'Rajasthan'])
dict_values(['Guwahati', 'Patna', 'Mumbai', 'Jaipur']) …
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.