PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Dictionary Methods: keys(), values(), items(), get() with Default (डिक्शनरी मेथड्स: keys(), values(), items() और get(key, default)) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python dictionaries provide dynamic view methods—keys(), values(), and items()—that reflect real-time dictionary mutations without duplicating data in memory. For safe lookups without raising KeyError on missing keys, dict.get(key, default=None) returns a fallback value while leaving the dictionary unmodified.
अध्याय सार: पायथन डिक्शनरी में keys(), values() और items() मेथड्स डायनामिक व्यू ऑब्जेक्ट्स लौटाते हैं जो बिना अतिरिक्त मेमोरी कॉपी बनाए डिक्शनरी में होने वाले परिवर्तनों को तुरंत दर्शाते हैं। अनुपस्थित कीज़ पर KeyError से बचने के लिए dict.get(key, default=None) का उपयोग किया जाता है जो डिक्शनरी को बदले बिना डिफ़ॉल्ट मान लौटाता है।
Dictionary Methods: keys(), values(), items(), get() with Default: Detailed Notes & Architecture (डिक्शनरी मेथड्स: keys(), values(), items() और get(key, default))
1. Dynamic Dictionary View Objects (keys, values, items) (डायनामिक डिक्शनरी व्यू ऑब्जेक्ट्स (keys, values, items))
[EN] Unlike Python 2 where these methods returned eager lists, Python 3's d.keys(), d.values(), and d.items() return lightweight view objects (dict_keys, dict_values, dict_items) in O(1) time. View objects act as live windows into the underlying hash table: adding or deleting keys in d immediately appears in previously captured views. They support len(), membership testing with in, iteration, and reversed() starting from Python 3.8, though they do not support indexing like d.keys()[0].
[HI] पायथन 2 में ये मेथड्स लिस्ट लौटाते थे, लेकिन पायथन 3 में d.keys(), d.values() और d.items() O(1) समय में हल्के व्यू ऑब्जेक्ट्स (dict_keys, dict_values, dict_items) लौटाते हैं। व्यू ऑब्जेक्ट मूल हैश टेबल की लाइव विंडो की तरह काम करते हैं: d में कोई भी नया जोड़ या विलोपन पहले से बने व्यू में तुरंत दिखता है। ये len(), in और इटरेशन का समर्थन करते हैं, परंतु d.keys()[0] जैसी इंडेक्सिंग स्वीकार नहीं करते।
2. Set-Like Operations on Views vs values() Limitations (व्यू ऑब्जेक्ट्स पर सेट-जैसे ऑपरेशंस और values() की सीमा)
[EN] Because dictionary keys are guaranteed unique and hashable, dict_keys supports set operations including intersection (&), union (|), difference (-), and symmetric difference (^). Similarly, dict_items supports set operations whenever all values inside the dictionary are hashable; if any value is a list or dict, set operations on items() raise TypeError. By contrast, dict_values never supports set operations because values may repeat and need not be hashable, and v in d.values() performs an O(n) linear scan.
[HI] चूँकि डिक्शनरी कीज़ अद्वितीय और हैशेबल होती हैं, इसलिए dict_keys पर इंटरसेक्शन (&), यूनियन (|), डिफरेंस (-) और सिमेट्रिक डिफरेंस (^) जैसे सेट ऑपरेशंस किए जा सकते हैं। इसी प्रकार यदि सभी वैल्यूज़ हैशेबल हों तो dict_items भी सेट ऑपरेशंस को सपोर्ट करता है। इसके विपरीत dict_values कभी भी सेट ऑपरेशंस को सपोर्ट नहीं करता क्योंकि वैल्यूज़ डुप्लीकेट या अनहैशेबल हो सकती हैं, और v in d.values() O(n) लीनियर सर्च करता है।
3. Safe Retrieval with get(key, default=None) (get(key, default=None) द्वारा सुरक्षित डेटा एक्सेस)
[EN] Accessing a missing key via bracket notation d[key] raises KeyError and terminates execution if unhandled. Calling d.get(key, default) returns the mapped value if key exists, or returns default (which itself defaults to None when omitted) without inserting anything into d. A subtle exam detail: if a key exists with an explicit value of None (e.g., {'a': None}), calling d.get('a', 'N/A') returns None rather than 'N/A' because the key is indeed present.
[HI] ब्रैकेट सिंटैक्स d[key] से अनुपस्थित की एक्सेस करने पर KeyError आता है। इसके स्थान पर d.get(key, default) कॉल करने से की मौजूद होने पर उसका मान मिलता है और न होने पर बिना डिक्शनरी बदले default (या न देने पर None) लौटता है। परीक्षा का एक सूक्ष्म बिंदु: यदि डिक्शनरी में {'a': None} मौजूद हो, तो d.get('a', 'N/A') 'N/A' के बजाय None लौटाएगा क्योंकि की 'a' वास्तव में उपस्थित है।
4. Comparing get() with setdefault() and Eager Evaluation (get() की setdefault() से तुलना और ईगर इवैल्यूएशन)
[EN] While get() is strictly read-only, d.setdefault(key, default) checks for key and—if absent—actually inserts d[key] = default into the dictionary before returning it. Notice also that in Python function calls, the default argument in d.get(key, expensive_call()) is evaluated eagerly BEFORE get() runs, even when key already exists in d. When computing defaults is costly or triggers side effects, an explicit if key in d check or collections.defaultdict is preferred.
[HI] जहाँ get() पूर्णतः रीड-ओनली है, वहीं d.setdefault(key, default) की अनुपस्थित होने पर डिक्शनरी में d[key] = default जोड़ भी देता है और वही मान लौटाता है। ध्यान रखें कि फ़ंक्शन कॉल नियम के कारण d.get(key, expensive_call()) में default आर्ग्युमेंट get() चलने से पहले ही ईगरली इवैल्यूएट हो जाता है, भले ही key पहले से मौजूद हो। महँगी गणना या साइड-इफेक्ट से बचने के लिए if key in d या defaultdict बेहतर विकल्प है।
Syntax, Formula & Technical Example (Dictionary Methods: keys(), values(), items(), get() with Default)
cfg = {'host': 'localhost', 'port': None}
kv = cfg.keys()
cfg['ssl'] = True
print(list(kv)) # Output: ['host', 'port', 'ssl']
print(cfg.get('timeout', 30)) # Output: 30
print(cfg.get('port', 8080)) # Output: None
print(cfg.keys() & {'ssl', 'db'}) # Output: {'ssl'}
Dictionary Methods: keys(), values(), items(), get() with Default Quick Reference & Comparison Table
| Method Signature | Return Value / Type | Key Exam Behavior |
|---|---|---|
| d.keys() | dict_keys dynamic view | Supports set operations like & and - |
| d.values() | dict_values dynamic view | Allows duplicates; no set operations |
| d.items() | dict_items of (key, value) | Yields 2-tuples; live updates reflected |
| d.get(k, default) | Value for k or default | Returns None if default omitted; no KeyError |
Dictionary Methods: keys(), values(), items(), get() with Default Top 20 Bilingual MCQs with Answers
Q1. In Python 3, what does type(d.keys()) return for a dictionary d?
(पायथन 3 में डिक्शनरी d के लिए type(d.keys()) क्या लौटाता है?)
(A) <class 'set'>
(B) <class 'list'>
(C) <class 'dict_keys'>
(D) <class 'tuple'>
Correct Answer: (C)
[EN] Python 3 returns a live dict_keys view object rather than creating a list copy.
[HI] पायथन 3 में लिस्ट की कॉपी बनाने के बजाय एक लाइव dict_keys व्यू ऑब्जेक्ट लौटाया जाता है।
Q2. What happens to an existing view v = d.keys() after executing d['new'] = 10?
(v = d.keys() बनाने के बाद d['new'] = 10 जोड़ने पर v पर क्या प्रभाव पड़ता है?)
(A) v automatically turns into None
(B) v stays frozen with the old keys
(C) v immediately reflects the new key 'new'
(D) Raises RuntimeError on next access to v
Correct Answer: (C)
[EN] Dictionary view objects are dynamic windows tied to the dictionary, so updates appear immediately.
[HI] डिक्शनरी व्यू ऑब्जेक्ट डायनामिक होते हैं, इसलिए डिक्शनरी में किया गया नया बदलाव व्यू में तुरंत दिखता है।
Q3. What does d.get('z') return when 'z' is not in d = {'a': 1}?
(d = {'a': 1} में 'z' न होने पर d.get('z') क्या लौटाता है?)
(A) None
(B) Raises KeyError
(C) False
(D) 0
Correct Answer: (A)
[EN] When the optional default parameter is omitted in get(), Python defaults it to None.
[HI] get() में दूसरा आर्ग्युमेंट न देने पर डिफ़ॉल्ट रूप से None लौटता है।
Q4. What is the output of d = {'x': None}; print(d.get('x', 50))?
(d = {'x': None}; print(d.get('x', 50)) का आउटपुट क्या होगा?)
(A) Raises KeyError
(B) 50
(C) None
(D) False
Correct Answer: (C)
[EN] Key 'x' exists in d with value None, so get() returns its stored value None rather than the fallback 50.
[HI] की 'x' डिक्शनरी में मौजूद है और उसका मान None है, इसलिए get() डिफ़ॉल्ट 50 के बजाय None लौटाता है।
Q5. What is the output of {'a': 1, 'b': 2}.keys() & {'b': 9, 'c': 3}.keys()?
({'a': 1, 'b': 2}.keys() & {'b': 9, 'c': 3}.keys() का आउटपुट क्या होगा?)
(A) {'b': 2}
(B) ['b']
(C) TypeError
(D) {'b'}
Correct Answer: (D)
[EN] dict_keys views support set intersection (&), yielding a set of common keys.
[HI] dict_keys व्यू सेट इंटरसेक्शन (&) को सपोर्ट करता है और समान कीज़ का सेट {'b'} लौटाता है।
Q6. Which expression raises a TypeError?
(निम्नलिखित में से कौन-सा एक्सप्रेशन TypeError उत्पन्न करेगा?)
(A) {'a': 1}.values() & {'b': 1}.values()
(B) {'a': 1}.keys() | {'b': 2}.keys()
(C) {'a': 1}.items() - {'b': 2}.items()
(D) list({'a': 1}.values())
Correct Answer: (A)
[EN] dict_values does not support set operations like & because dictionary values are neither required to be unique nor hashable.
[HI] dict_values सेट ऑपरेशंस (&) को सपोर्ट नहीं करता क्योंकि वैल्यूज़ का अद्वितीय या हैशेबल होना आवश्यक नहीं होता।
Q7. What does d.keys()[0] do on d = {'a': 1, 'b': 2}?
(d = {'a': 1, 'b': 2} पर d.keys()[0] चलाने पर क्या होता है?)
(A) Returns 1
(B) Returns 'a'
(C) Returns ('a', 1)
(D) Raises TypeError: 'dict_keys' object is not subscriptable
Correct Answer: (D)
[EN] Dictionary views do not support integer indexing directly; convert to a list via list(d.keys())[0] first.
[HI] डिक्शनरी व्यू सीधे इंडेक्सिंग सपोर्ट नहीं करते; इसके लिए पहले list(d.keys())[0] करना होता है।
Q8. How does d.setdefault('k', 10) differ from d.get('k', 10) when 'k' is missing?
(जब 'k' अनुपस्थित हो, तो d.setdefault('k', 10) और d.get('k', 10) में क्या अंतर है?)
(A) setdefault inserts 'k': 10 into d, whereas get leaves d unchanged
(B) get inserts 'k': 10 into d, whereas setdefault does not
(C) Both modify d identically
(D) setdefault raises KeyError when 'k' is missing
Correct Answer: (A)
[EN] get() is read-only; setdefault() inserts the key with the default value if the key is absent.
[HI] get() केवल रीड-ओनली है; setdefault() की न होने पर डिक्शनरी में डिफ़ॉल्ट वैल्यू के साथ की जोड़ देता है।
Q9. What type of items does iterating over d.items() produce?
(d.items() पर इटरेट करने पर किस प्रकार के तत्व प्राप्त होते हैं?)
(A) Strings formatted as 'key:value'
(B) Dictionaries of length 1
(C) 2-tuples of (key, value)
(D) Lists of [key, value]
Correct Answer: (C)
[EN] Each element yielded by d.items() is a tuple (key, value) ready for tuple unpacking.
[HI] d.items() प्रत्येक जोड़े को (key, value) के 2-टपल के रूप में लौटाता है।
Q10. What is the time complexity of checking val in d.values() for a dictionary of n entries?
(n एंट्री वाली डिक्शनरी में val in d.values() जाँचने की समय जटिलता क्या है?)
(A) O(log n)
(B) O(1)
(C) O(n)
(D) O(n log n)
Correct Answer: (C)
[EN] Values are not indexed by hash, so checking membership in d.values() requires scanning up to n values linearly.
[HI] वैल्यूज़ हैश द्वारा इंडेक्स नहीं होतीं, इसलिए d.values() में खोज O(n) लीनियर समय लेती है।
Q11. From which Python version can reversed(d.keys()) and reversed(d.items()) be used directly?
(किस पायथन संस्करण से reversed(d.keys()) और reversed(d.items()) का सीधा उपयोग संभव हुआ?)
(A) Python 3.8
(B) Python 3.4
(C) Python 3.11
(D) Python 3.6
Correct Answer: (A)
[EN] Reverse iteration support for dicts and dict views (__reversed__) was added in Python 3.8.
[HI] डिक्शनरी और उसके व्यू ऑब्जेक्ट्स पर रिवर्स इटरेशन (__reversed__) पायथन 3.8 में जोड़ा गया।
Q12. When does d1.items() & d2.items() raise a TypeError?
(d1.items() & d2.items() कब TypeError उत्पन्न करता है?)
(A) When dictionaries have different lengths
(B) When keys are strings
(C) When any value in either dictionary is unhashable (e.g., a list)
(D) Always, because items() never supports &
Correct Answer: (C)
[EN] Set operations on dict_items require each (key, value) tuple to be hashable, which fails if any value is unhashable.
[HI] dict_items पर सेट ऑपरेशन के लिए प्रत्येक (key, value) टपल का हैशेबल होना आवश्यक है, जो लिस्ट वैल्यू होने पर विफल हो जाता है।
Q13. What does d = {'a': 1}; d['b'] raise?
(d = {'a': 1}; d['b'] कौन-सा अपवाद देता है?)
(A) IndexError
(B) AttributeError
(C) KeyError
(D) None
Correct Answer: (C)
[EN] Direct indexing with a missing key raises KeyError rather than returning None.
[HI] अनुपस्थित की को सीधे ब्रैकेट से एक्सेस करने पर None के बजाय KeyError आता है।
Q14. If d = {'a': 1}, what happens in d.get('a', print('ran'))?
(यदि d = {'a': 1} हो, तो d.get('a', print('ran')) चलाने पर क्या होगा?)
(A) It prints 'ran' and returns None
(B) It prints 'ran' and returns 1
(C) It returns None
(D) It skips print('ran') and returns 1
Correct Answer: (B)
[EN] Python evaluates all function arguments eagerly before entering get(), so print('ran') runs even though 'a' exists.
[HI] पायथन get() में प्रवेश से पहले सभी आर्ग्युमेंट्स का मूल्यांकन करता है, इसलिए 'a' होने पर भी print('ran') चलेगा।
Q15. How do you obtain a standard list of all values from dictionary d?
(डिक्शनरी d की सभी वैल्यूज़ की मानक लिस्ट कैसे प्राप्त की जाती है?)
(A) set(d.values())
(B) d.values().tolist()
(C) list(d.values())
(D) d.values[:]
Correct Answer: (C)
[EN] Passing the dict_values view to list() materializes its elements into a list in insertion order.
[HI] dict_values व्यू को list() में पास करने पर प्रविष्टि क्रम में सभी वैल्यूज़ की लिस्ट बन जाती है।
Q16. Which statement is NOT true about Python 3 dictionary view objects?
(पायथन 3 के डिक्शनरी व्यू ऑब्जेक्ट्स के बारे में कौन-सा कथन सत्य नहीं है?)
(A) Dictionary views snapshot the keys at creation time and ignore later edits
(B) d.keys() supports & and | operations
(C) d.values() can contain duplicate items
(D) len(d.keys()) returns the current entry count
Correct Answer: (A)
[EN] Dictionary views do NOT snapshot data; they remain dynamically linked to the dictionary and reflect subsequent mutations.
[HI] डिक्शनरी व्यू स्नैपशॉट नहीं बनाते; वे मूल डिक्शनरी से जुड़े रहकर बाद के सभी बदलाव तुरंत दिखाते हैं।
Q17. Within Dictionary Methods: keys(), values(), items(), get() with Default, which method signature directly maps to: "dict_keys dynamic view"?
(Dictionary Methods: keys(), values(), items(), get() with Default में कौन सा Method Signature सीधे "dict_keys dynamic view" से संबंधित है?)
(A) d.keys()
(B) d.get(k, default)
(C) d.items()
(D) d.values()
Correct Answer: (A)
[EN] In Dictionary Methods: keys(), values(), items(), get() with Default, 'd.keys()' handles 'dict_keys dynamic view' (Supports set operations like & and -).
[HI] Dictionary Methods: keys(), values(), items(), get() with Default में 'd.keys()' का कार्य/विवरण 'dict_keys dynamic view' (Supports set operations like & and -) है।
Q18. Which option accurately describes 'd.values()' in Dictionary Methods: keys(), values(), items(), get() with Default?
(Dictionary Methods: keys(), values(), items(), get() with Default के संदर्भ में 'd.values()' की सही विशेषता कौन सी है?)
(A) dict_items of (key, value)
(B) Value for k or default
(C) dict_keys dynamic view
(D) dict_values dynamic view
Correct Answer: (D)
[EN] 'd.values()' provides 'dict_values dynamic view' — key point: Allows duplicates; no set operations.
[HI] 'd.values()' का मुख्य विवरण 'dict_values dynamic view' है (Allows duplicates; no set operations)।
Q19. In Dictionary Methods: keys(), values(), items(), get() with Default, identify the correct parameter for the specification: "dict_items of (key, value)".
(Dictionary Methods: keys(), values(), items(), get() with Default में "dict_items of (key, value)" विवरण किस विकल्प के लिए सही है?)
(A) d.keys()
(B) d.items()
(C) d.get(k, default)
(D) d.values()
Correct Answer: (B)
[EN] 'd.items()' corresponds to 'dict_items of (key, value)' (Yields 2-tuples; live updates reflected).
[HI] 'd.items()' का संबंध 'dict_items of (key, value)' (Yields 2-tuples; live updates reflected) से है।
Q20. Considering Dictionary Methods: keys(), values(), items(), get() with Default, what key behavior defines 'd.get(k, default)'?
(Dictionary Methods: keys(), values(), items(), get() with Default में 'd.get(k, default)' की मुख्य भूमिका या मान क्या है?)
(A) dict_keys dynamic view
(B) dict_items of (key, value)
(C) dict_values dynamic view
(D) Value for k or default
Correct Answer: (D)
[EN] 'd.get(k, default)' represents 'Value for k or default' (Returns None if default omitted; no KeyError).
[HI] 'd.get(k, default)' का अर्थ/मान 'Value for k or default' है (Returns None if default omitted; no KeyError)।
Dictionary Methods: keys(), values(), items(), get() with Default — Exam FAQs
Q1: Why did Python 3 change keys(), values(), and items() from lists to view objects?
पायथन 3 ने keys(), values() और items() को लिस्ट से बदलकर व्यू ऑब्जेक्ट क्यों बनाया?
[EN] Returning view objects avoids allocating O(n) extra memory for temporary lists during loops and allows O(1) creation plus set-algebra support on keys.
[HI] व्यू ऑब्जेक्ट लौटाने से लूप चलाते समय अस्थायी लिस्ट के लिए O(n) अतिरिक्त मेमोरी बर्बाद नहीं होती और O(1) निर्माण के साथ सेट ऑपरेशंस भी मिलते हैं।
Q2: When should I use d.get(k, default) vs d[k]?
d.get(k, default) और d[k] का उपयोग कब करना चाहिए?
[EN] Use d.get() when missing keys are normal and a safe fallback is desired; use d[k] when a missing key indicates a bug or contract violation that should fail fast with KeyError.
[HI] जब की का न मिलना सामान्य स्थिति हो और डिफ़ॉल्ट मान चाहिए तब d.get() का उपयोग करें; जब की का न होना किसी बग या त्रुटि का संकेत हो तब d[k] बेहतर है।
Q3: Can I modify a dictionary while iterating over d.keys() or d.items()?
क्या d.keys() या d.items() पर लूप चलाते समय डिक्शनरी का आकार बदला जा सकता है?
[EN] No. Adding or deleting keys while iterating over a dictionary view raises RuntimeError: dictionary changed size during iteration. Iterate over list(d.keys()) instead.
[HI] नहीं। व्यू पर इटरेट करते समय की जोड़ने या हटाने से RuntimeError: dictionary changed size during iteration आता है; इसके लिए list(d.keys()) पर लूप चलाएं।
Post a Comment
Post a Comment