PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Python Dictionaries: Key-Value Pairs, Hash Table Architecture (पायथन डिक्शनरी: की-वैल्यू जोड़े, हैश मैप और म्यूटेबल प्रकृति) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: A Python dict is a mutable, dynamic mapping type that stores unique hashable keys associated with arbitrary values using an open-addressing hash table. Since Python 3.7, dictionaries guarantee key insertion order as part of the official language specification while offering average O(1) lookup, insertion, and deletion.
अध्याय सार: पायथन dict एक म्यूटेबल (परिवर्तनशील) और डायनामिक मैपिंग डेटा टाइप है जो ओपन-एड्रेसिंग हैश टेबल की मदद से अद्वितीय हैशेबल कीज़ (keys) को मनचाहे मानों (values) से जोड़कर संग्रहीत करता है। पायथन 3.7 से डिक्शनरी आधिकारिक रूप से प्रविष्टि क्रम (insertion order) की गारंटी देती है तथा औसत O(1) समय में खोज, प्रविष्टि और विलोपन प्रदान करती है।
Python Dictionaries: Key-Value Pairs, Hash Table Architecture: Detailed Notes & Architecture (पायथन डिक्शनरी: की-वैल्यू जोड़े, हैश मैप और म्यूटेबल प्रकृति)
1. Key-Value Mapping and Creation Syntax (की-वैल्यू मैपिंग और निर्माण सिंटैक्स)
[EN] Dictionaries map unique keys to values and can be instantiated via curly-brace literals {'k': 1}, the dict() constructor with keyword arguments dict(k=1), or an iterable of 2-tuples dict([('k', 1)]). Empty curly braces {} always allocate an empty dict rather than a set. If duplicate keys appear in a literal such as {'a': 1, 'b': 2, 'a': 9}, the later value overwrites the earlier one while the key retains its original insertion position ('a' stays first).
[HI] डिक्शनरी अद्वितीय कीज़ को मानों से मैप करती है और इसे {'k': 1} लिटरल, dict(k=1) कीवर्ड आर्ग्युमेंट्स या dict([('k', 1)]) जैसे 2-टपल इटरेबल से बनाया जा सकता है। खाली {} हमेशा एक खाली dict बनाता है, न कि set। यदि लिटरल में डुप्लीकेट की आए जैसे {'a': 1, 'b': 2, 'a': 9}, तो बाद वाला मान पुराने को ओवरराइट कर देता है जबकि की अपने मूल प्रविष्टि स्थान पर ही बनी रहती है।
2. Compact Hash Table Architecture and Open Addressing (कॉम्पैक्ट हैश टेबल आर्किटेक्चर और ओपन एड्रेसिंग)
[EN] Modern CPython (3.6+) implements dictionaries using a compact hash table layout with two internal arrays: a sparse indices array storing table slots and a dense entries array storing (hash, key, value) triplets in insertion order. Unlike Java HashMap, CPython does not use separate chaining with linked lists; instead, it resolves hash collisions via open addressing with pseudo-random probing. When the table reaches approximately two-thirds (2/3) load factor, Python automatically allocates a larger array and rehashes existing entries.
[HI] आधुनिक CPython (3.6+) में डिक्शनरी दो आंतरिक ऐरे वाले कॉम्पैक्ट हैश टेबल डिज़ाइन का उपयोग करती है: एक स्पार्स indices ऐरे जो स्लॉट क्रमांक रखता है और एक सघन (dense) entries ऐरे जो (hash, key, value) को प्रविष्टि क्रम में सहेजता है। जावा HashMap के विपरीत, CPython लिंक्ड लिस्ट सेपरेट चेनिंग के बजाय सूडो-रैंडम प्रोबिंग के साथ ओपन एड्रेसिंग द्वारा हैश कोलिजन हल करता है। जब टेबल लगभग दो-तिहाई (2/3) लोड फैक्टर तक भर जाती है, तो पायथन स्वतः बड़ा ऐरे बनाकर री-हैशिंग करता है।
3. Time Complexity and Insertion Order Guarantee (टाइम कॉम्प्लेक्सिटी और इन्सर्शन ऑर्डर गारंटी)
[EN] Average-case time complexity for lookup (d[key]), insertion (d[key] = val), deletion (del d[key]), and membership test (key in d) is O(1), though pathological hash collisions or table resizing can degrade a single operation to O(n). Key insertion order was preserved as an implementation detail in CPython 3.6 and became a guaranteed language feature in Python 3.7+. Note that equality comparison d1 == d2 checks matching key-value pairs regardless of insertion order.
[HI] डेटा खोज (d[key]), प्रविष्टि (d[key] = val), विलोपन (del d[key]) और सदस्यता जाँच (key in d) की औसत समय जटिलता O(1) होती है, यद्यपि गंभीर हैश टकराव या री-साइजिंग में वर्स्ट-केस O(n) हो सकता है। प्रविष्टि क्रम CPython 3.6 में एक इम्प्लीमेंटेशन विवरण था जिसे पायथन 3.7+ में भाषा की आधिकारिक गारंटी बना दिया गया। ध्यान रहे कि d1 == d2 तुलना करते समय केवल की-वैल्यू जोड़ों की समानता देखी जाती है, उनके क्रम की नहीं।
4. Mutability, Aliases, and Value Flexibility (म्यूटेबिलिटी, एलियास और वैल्यू लचीलापन)
[EN] While dictionary keys must be hashable, values have zero restrictions: they may be duplicates, mutable lists, functions, None, or nested dictionaries. Because dictionaries are mutable objects passed by object reference, assigning d2 = d1 creates an alias to the same hash table rather than an independent copy; mutating d2 directly affects d1 unless d1.copy() or copy.deepcopy() is used. Deleting entries marks slots as dummy entries in the sparse index table until a resize compacts the dense array.
[HI] जहाँ डिक्शनरी कीज़ का हैशेबल होना अनिवार्य है, वहीं वैल्यूज़ पर कोई प्रतिबंध नहीं है; वे डुप्लीकेट, म्यूटेबल लिस्ट, फ़ंक्शन, None या नेस्टेड डिक्शनरी हो सकती हैं। चूँकि डिक्शनरी म्यूटेबल है और ऑब्जेक्ट रेफरेंस द्वारा पास होती है, d2 = d1 करने पर नई कॉपी के बजाय उसी हैश टेबल का एलियास बनता है; स्वतंत्र कॉपी के लिए d1.copy() या copy.deepcopy() आवश्यक है। किसी की को डिलीट करने पर स्पार्स इंडेक्स में डमी स्लॉट चिह्नित होता है जिसे अगले री-साइज पर साफ किया जाता है।
Syntax, Formula & Technical Example (Python Dictionaries: Key-Value Pairs, Hash Table Architecture)
scores = {'ram': 80, 'sita': 92, 'ram': 95}
print(list(scores.keys())) # Output: ['ram', 'sita']
print(scores['ram']) # Output: 95
other = {'sita': 92, 'ram': 95}
print(scores == other) # Output: True
print('sita' in scores, 92 in scores) # Output: True False
print(type({})) # Output: <class 'dict'>
Python Dictionaries: Key-Value Pairs, Hash Table Architecture Quick Reference & Comparison Table
| Dictionary Aspect | Technical Specification | Exam Fact |
|---|---|---|
| Empty braces {} | Creates empty dict object | Use set() for an empty set |
| Insertion order | Guaranteed since Python 3.7 | Implementation detail in CPython 3.6 |
| Average lookup time | O(1) via open addressing | Worst case O(n) on hash collisions |
| Hash table resizing | Triggered near 2/3 load factor | Uses dense entries plus sparse indices |
Python Dictionaries: Key-Value Pairs, Hash Table Architecture Top 20 Bilingual MCQs with Answers
Q1. What object does type({}) return in Python 3?
(पायथन 3 में type({}) क्या लौटाता है?)
(A) <class 'frozenset'>
(B) <class 'tuple'>
(C) <class 'set'>
(D) <class 'dict'>
Correct Answer: (D)
[EN] Empty curly braces {} are reserved for creating an empty dictionary; an empty set requires set().
[HI] खाली मझला कोष्ठक {} हमेशा खाली डिक्शनरी बनाता है; खाली सेट के लिए set() लिखना आवश्यक है।
Q2. From which Python version did preserving dictionary insertion order become an official language specification?
(किस पायथन संस्करण से डिक्शनरी में प्रविष्टि क्रम (insertion order) को आधिकारिक भाषा नियम बनाया गया?)
(A) Python 3.8
(B) Python 3.7
(C) Python 3.5
(D) Python 2.7
Correct Answer: (B)
[EN] Insertion order was a CPython 3.6 implementation detail and became guaranteed across all Python implementations in Python 3.7.
[HI] प्रविष्टि क्रम CPython 3.6 में केवल आंतरिक विवरण था और पायथन 3.7 से इसे आधिकारिक मानक बना दिया गया।
Q3. Which collision resolution technique does CPython's dictionary hash table use?
(CPython की डिक्शनरी हैश टेबल टकराव (collision) हल करने के लिए किस तकनीक का उपयोग करती है?)
(A) Linear array sorting
(B) Open addressing with pseudo-random probing
(C) Separate chaining with linked lists
(D) Binary search trees per bucket
Correct Answer: (B)
[EN] CPython resolves hash collisions using open addressing with pseudo-random probing rather than linked-list chaining.
[HI] CPython लिंक्ड लिस्ट चेनिंग के बजाय सूडो-रैंडम प्रोबिंग के साथ ओपन एड्रेसिंग का उपयोग करता है।
Q4. What is the output of d = {'x': 1, 'y': 2, 'x': 5}; print(list(d.items()))?
(d = {'x': 1, 'y': 2, 'x': 5}; print(list(d.items())) का आउटपुट क्या होगा?)
(A) [('y', 2), ('x', 5)]
(B) Raises KeyError
(C) [('x', 5), ('y', 2)]
(D) [('x', 1), ('y', 2), ('x', 5)]
Correct Answer: (C)
[EN] Duplicate keys overwrite the existing value (1 becomes 5) while keeping 'x' at its original first insertion slot.
[HI] डुप्लीकेट की पुराने मान को बदलकर 5 कर देती है, लेकिन 'x' अपने पहले वाले प्रविष्टि स्थान पर ही रहता है।
Q5. What does {'a': 1, 'b': 2} == {'b': 2, 'a': 1} evaluate to in Python 3.12?
(पायथन 3.12 में {'a': 1, 'b': 2} == {'b': 2, 'a': 1} का मान क्या होगा?)
(A) False
(B) TypeError
(C) None
(D) True
Correct Answer: (D)
[EN] Dictionary equality compares key-value mappings regardless of key order, even though dictionaries preserve insertion order.
[HI] डिक्शनरी समानता (==) क्रम के बजाय केवल की-वैल्यू जोड़ों के मिलान की जाँच करती है, भले ही क्रम सुरक्षित रहता हो।
Q6. In d = {'name': 'Anita', 'age': 24}, what does 'Anita' in d return?
(d = {'name': 'Anita', 'age': 24} में 'Anita' in d क्या लौटाता है?)
(A) False
(B) True
(C) None
(D) Raises KeyError
Correct Answer: (A)
[EN] The in operator on a dictionary searches only the keys in O(1) time, not the values.
[HI] डिक्शनरी पर in ऑपरेटर केवल कीज़ में खोज करता है, वैल्यूज़ में नहीं।
Q7. Approximately at what load factor does CPython resize its dictionary hash table?
(CPython लगभग किस लोड फैक्टर पर डिक्शनरी हैश टेबल को री-साइज करता है?)
(A) 2/3 (about 66.7%)
(B) 1/10 (10%)
(C) 1/4 (25%)
(D) 1.0 (100%)
Correct Answer: (A)
[EN] CPython triggers resizing when roughly 2/3 of the hash table slots are occupied to keep probing sequences short.
[HI] प्रोबिंग को तेज़ रखने के लिए CPython लगभग 2/3 (66.7%) स्लॉट भरने पर हैश टेबल री-साइज करता है।
Q8. What is the average time complexity of accessing d[key] in a Python dictionary of n items?
(n आइटम वाली पायथन डिक्शनरी में d[key] को एक्सेस करने की औसत समय जटिलता क्या है?)
(A) O(n^2)
(B) O(1)
(C) O(n)
(D) O(log n)
Correct Answer: (B)
[EN] Hash indexing allows direct slot computation in constant O(1) average time.
[HI] हैश इंडेक्सिंग के कारण औसत रूप से स्थिर O(1) समय में सीधे स्लॉट से मान मिल जाता है।
Q9. What is the result of dict([('a', 10), ('b', 20)])?
(dict([('a', 10), ('b', 20)]) का परिणाम क्या होगा?)
(A) {10: 'a', 20: 'b'}
(B) {'a': 10, 'b': 20}
(C) [('a', 10), ('b', 20)]
(D) Raises TypeError
Correct Answer: (B)
[EN] The dict() constructor builds a dictionary from any iterable of two-element key-value pairs.
[HI] dict() कन्स्ट्रक्टर दो-तत्व वाले की-वैल्यू जोड़ों के किसी भी इटरेबल से डिक्शनरी बना देता है।
Q10. What happens when you run a = {'k': 1}; b = a; b['k'] = 99; print(a['k'])?
(a = {'k': 1}; b = a; b['k'] = 99; print(a['k']) चलाने पर क्या प्रिंट होगा?)
(A) 1
(B) Raises KeyError
(C) None
(D) 99
Correct Answer: (D)
[EN] Assignment b = a binds both names to the same mutable dictionary object, so mutating b updates a as well.
[HI] b = a दोनों वेरिएबल्स को एक ही म्यूटेबल डिक्शनरी से जोड़ता है, अतः b में बदलाव a में भी दिखता है।
Q11. Which component was introduced in CPython 3.6 compact dictionaries to reduce memory overhead?
(मेमोरी खपत घटाने के लिए CPython 3.6 कॉम्पैक्ट डिक्शनरी में कौन-सी संरचना अपनाई गई?)
(A) A sparse indices array referencing a dense entries array
(B) A doubly linked list per key
(C) A separate file-backed index
(D) A balanced red-black tree
Correct Answer: (A)
[EN] Separating sparse hash indices from the dense array of (hash, key, value) entries cuts memory usage by ~20-25% and preserves insertion order.
[HI] स्पार्स हैश इंडेक्स को सघन entries ऐरे से अलग रखने से लगभग 20-25% मेमोरी बचती है और प्रविष्टि क्रम सुरक्षित रहता है।
Q12. Can two different keys in the same Python dictionary map to the exact same mutable list object?
(क्या एक ही पायथन डिक्शनरी की दो अलग-अलग कीज़ एक ही म्यूटेबल लिस्ट को वैल्यू के रूप में रख सकती हैं?)
(A) Yes, dictionary values can be duplicated and mutable
(B) No, values cannot be mutable
(C) No, values must also be unique
(D) Only if keys are integers
Correct Answer: (A)
[EN] Uniqueness and hashability restrictions apply solely to keys; values can be duplicates and any mutable or immutable object.
[HI] अद्वितीयता और हैशेबिलिटी की शर्त केवल कीज़ पर लागू होती है; वैल्यूज़ डुप्लीकेट और म्यूटेबल दोनों हो सकती हैं।
Q13. What does dict(one=1, two=2) produce?
(dict(one=1, two=2) क्या बनाता है?)
(A) {'one': 1, 'two': 2}
(B) {'1': 'one', '2': 'two'}
(C) Raises SyntaxError
(D) {one: 1, two: 2}
Correct Answer: (A)
[EN] Keyword arguments passed to dict() create string keys matching the parameter names.
[HI] dict() में दिए गए कीवर्ड आर्ग्युमेंट्स पैरामीटर नामों को स्ट्रिंग कीज़ के रूप में उपयोग करके डिक्शनरी बनाते हैं।
Q14. What happens if you pass a 3-element tuple inside a list to dict([('a', 1, 2)])?
(dict([('a', 1, 2)]) चलाने पर क्या होता है?)
(A) Ignores the third element
(B) Raises ValueError
(C) Raises KeyError
(D) Creates {'a': (1, 2)}
Correct Answer: (B)
[EN] Each item in the iterable passed to dict() must unpack into exactly 2 elements (key and value); 3 elements trigger ValueError.
[HI] dict() को दिए जाने वाले प्रत्येक आइटम में ठीक 2 तत्व (key, value) होने चाहिए; 3 तत्व होने पर ValueError आता है।
Q15. What is the worst-case time complexity of a dictionary lookup in Python?
(पायथन डिक्शनरी में लुकअप की वर्स्ट-केस (worst-case) समय जटिलता क्या है?)
(A) O(n!)
(B) O(n)
(C) O(1)
(D) O(log n)
Correct Answer: (B)
[EN] If many keys collide onto overlapping probe chains, searching can degrade linearly to O(n).
[HI] यदि कई कीज़ एक ही प्रोब श्रृंखला पर टकराती हैं, तो खोज की जटिलता O(n) तक जा सकती है।
Q16. Which statement is NOT true about Python dictionaries?
(पायथन डिक्शनरी के बारे में कौन-सा कथन सत्य नहीं है?)
(A) Dictionaries are mutable mappings
(B) Values can be heterogeneous types
(C) Keys must be unique within one dict
(D) Dictionaries support integer slicing like d[0:2]
Correct Answer: (D)
[EN] Dictionaries are keyed mappings, not positional sequences, so slicing like d[0:2] raises TypeError: unhashable type: 'slice'.
[HI] डिक्शनरी की-आधारित मैपिंग है न कि सीक्वेंस, इसलिए d[0:2] स्लाइसिंग करने पर TypeError आता है।
Q17. Within Python Dictionaries: Key-Value Pairs, Hash Table Architecture, which dictionary aspect directly maps to: "Creates empty dict object"?
(Python Dictionaries: Key-Value Pairs, Hash Table Architecture में कौन सा Dictionary Aspect सीधे "Creates empty dict object" से संबंधित है?)
(A) Average lookup time
(B) Hash table resizing
(C) Insertion order
(D) Empty braces {}
Correct Answer: (D)
[EN] In Python Dictionaries: Key-Value Pairs, Hash Table Architecture, 'Empty braces {}' handles 'Creates empty dict object' (Use set() for an empty set).
[HI] Python Dictionaries: Key-Value Pairs, Hash Table Architecture में 'Empty braces {}' का कार्य/विवरण 'Creates empty dict object' (Use set() for an empty set) है।
Q18. Which option accurately describes 'Insertion order' in Python Dictionaries: Key-Value Pairs, Hash Table Architecture?
(Python Dictionaries: Key-Value Pairs, Hash Table Architecture के संदर्भ में 'Insertion order' की सही विशेषता कौन सी है?)
(A) Creates empty dict object
(B) Guaranteed since Python 3.7
(C) O(1) via open addressing
(D) Triggered near 2/3 load factor
Correct Answer: (B)
[EN] 'Insertion order' provides 'Guaranteed since Python 3.7' — key point: Implementation detail in CPython 3.6.
[HI] 'Insertion order' का मुख्य विवरण 'Guaranteed since Python 3.7' है (Implementation detail in CPython 3.6)।
Q19. In Python Dictionaries: Key-Value Pairs, Hash Table Architecture, identify the correct parameter for the specification: "O(1) via open addressing".
(Python Dictionaries: Key-Value Pairs, Hash Table Architecture में "O(1) via open addressing" विवरण किस विकल्प के लिए सही है?)
(A) Hash table resizing
(B) Average lookup time
(C) Empty braces {}
(D) Insertion order
Correct Answer: (B)
[EN] 'Average lookup time' corresponds to 'O(1) via open addressing' (Worst case O(n) on hash collisions).
[HI] 'Average lookup time' का संबंध 'O(1) via open addressing' (Worst case O(n) on hash collisions) से है।
Q20. Considering Python Dictionaries: Key-Value Pairs, Hash Table Architecture, what key behavior defines 'Hash table resizing'?
(Python Dictionaries: Key-Value Pairs, Hash Table Architecture में 'Hash table resizing' की मुख्य भूमिका या मान क्या है?)
(A) O(1) via open addressing
(B) Triggered near 2/3 load factor
(C) Creates empty dict object
(D) Guaranteed since Python 3.7
Correct Answer: (B)
[EN] 'Hash table resizing' represents 'Triggered near 2/3 load factor' (Uses dense entries plus sparse indices).
[HI] 'Hash table resizing' का अर्थ/मान 'Triggered near 2/3 load factor' है (Uses dense entries plus sparse indices)।
Python Dictionaries: Key-Value Pairs, Hash Table Architecture — Exam FAQs
Q1: Why does {} create an empty dictionary instead of an empty set in Python?
पायथन में {} खाली सेट के बजाय खाली डिक्शनरी क्यों बनाता है?
[EN] Dictionaries existed in Python from its earliest versions long before sets were added in Python 2.3/2.4, so Python preserved backward compatibility by keeping {} for dict and requiring set() for an empty set.
[HI] डिक्शनरी पायथन के शुरुआती संस्करणों से ही मौजूद थी जबकि सेट बाद में जोड़ा गया; बैकवर्ड कम्पैटिबिलिटी बनाए रखने के लिए {} को dict हेतु सुरक्षित रखा गया और खाली सेट के लिए set() तय किया गया।
Q2: Does dict insertion order affect dictionary equality comparison?
क्या डिक्शनरी में कीज़ के प्रविष्टि क्रम का असर == समानता तुलना पर पड़ता है?
[EN] No. Two dictionaries with identical key-value pairs evaluate to True under == even if their keys were inserted in different orders.
[HI] नहीं। यदि दो डिक्शनरियों में समान की-वैल्यू जोड़े हैं, तो प्रविष्टि क्रम अलग होने पर भी == तुलना True लौटाती है।
Q3: Are Python dictionaries thread-safe for concurrent writes?
क्या पायथन डिक्शनरी एक साथ मल्टी-थ्रेडेड राइट्स के लिए थ्रेड-सेफ है?
[EN] Simple single C-level operations are protected by the GIL, but compound read-modify-write patterns like d[k] = d.get(k, 0) + 1 are not atomic and still require threading.Lock.
[HI] सरल एकल ऑपरेशन GIL द्वारा सुरक्षित रहते हैं, लेकिन d[k] = d.get(k, 0) + 1 जैसे संयुक्त रीड-मॉडिफाई-राइट ऑपरेशन एटॉमिक नहीं हैं और इनके लिए threading.Lock आवश्यक होता है।
Post a Comment
Post a Comment