← Back to Python Course | Chapter 6: Data Structures | Lesson 12 of 12

Python Nested Dictionaries

Dictionary की values कुछ भी हो सकती हैं — यहाँ तक कि एक और dictionary भी — और Python में hierarchical, structured data represent करने का यही तरीका है: dictionaries की एक dictionary, जैसे user profiles का एक collection जिसमें हर profile के अपने name, age, और address fields हों, real-world nested data को स्वाभाविक रूप से mirror करता है।
Syntax
python
nested_dict = {
    key1: {inner_key: value},
    key2: {inner_key: value}
}
nested_dict[key1][inner_key]

Nested Dictionary बनाना

Nested dictionary बिल्कुल एक regular dictionary literal की तरह लिखी जाती है, बस इसकी एक या ज़्यादा values खुद dictionary literals होती हैं — {"sam": {"age": 28, "city": "Austin"}} एक outer dictionary के अंदर, username से keyed, एक level nested single user का data represent करता है।

Note: Nested dictionary literal को consistent indentation के साथ कई lines में format कीजिए, ताकि इसकी hierarchical structure एक नज़र में पढ़ने लायक रहे।
Warning: अगर clear indentation के साथ format न किया जाए तो कई levels वाली deeply nested dictionary literal visually पढ़ने में मुश्किल हो सकती है।

उदाहरण: Creating a Nested Dictionary

python
data = {"sam": {"age": 28, "city": "Austin"}}
print(data)

Nested Values को Access करना

किसी deeply nested value तक पहुँचने का मतलब है एक-एक करके square-bracket lookups chain करना — data["users"]["sam"]["address"]["city"] नेस्टिंग के तीन levels से गुज़रकर, structure में nested होने के ठीक उसी क्रम में, final value तक पहुँचता है।

Note: किसी chained nested lookup को left से right "पहले इस key के अंदर जाओ, फिर उस key के अंदर, फिर उस अगली में" की तरह पढ़िए, ताकि पता रहे आप किस level को navigate कर रहे हैं।
Warning: Chain में किसी भी level (सिर्फ आखिरी में नहीं) पर missing key उसी specific level पर KeyError raise करती है, जो confusing हो सकता है अगर आपने माना हो कि सिर्फ आखिरी key ही missing हो सकती है।

उदाहरण: Accessing Nested Values

python
data = {"users": {"sam": {"address": {"city": "Austin"}}}}
print(data["users"]["sam"]["address"]["city"])

Nested Dictionary Values को Modify करना

Nested dictionary के अंदर किसी value को update करना उसे पढ़ने वाला ही chained bracket syntax इस्तेमाल करता है — data["users"]["sam"]["age"] = 29 nested structure के अंदर जाकर सिर्फ उस एक specific value को update करता है, बाकी structure को बिना छुए छोड़ देता है।

Note: किसी deeply nested value को modify करने से पहले confirm कर लीजिए कि हर intermediate level पहले से मौजूद है, वरना assignment missing levels को अपने-आप बनाने की बजाय KeyError raise करेगा।
Warning: कुछ languages के विपरीत, Python जब आप किसी deeply nested path पर assign करते हैं तो missing intermediate dictionaries को अपने-आप नहीं बनाता — हर level पहले से मौजूद होनी चाहिए।

उदाहरण: Modifying Nested Dictionary Values

python
data = {"users": {"sam": {"age": 28}}}
data["users"]["sam"]["age"] = 29  # chained brackets reach into the nested dict
print(data)

Nested Dictionary पर Loop करना

Nested dictionary पर loop करने का मतलब आमतौर पर top-level keys पर एक outer loop होता है, जिसके अंदर एक inner loop (या direct access) हर nested dictionary की अपनी keys और values को handle करता है — जिससे आप हर टुकड़े hierarchical data को व्यवस्थित रूप से process कर सकें।

Note: loop के हर level पर .items() इस्तेमाल कीजिए ताकि key और nested value दोनों साथ मिलें, बजाय इसके कि सिर्फ key लेकर value अलग से लुकअप करें।
Warning: यह भूल जाना कि outer level की हर iteration की value खुद एक dictionary है (plain value नहीं), nested data पर पहली बार loop करते समय confusion का आम कारण है।

उदाहरण: Looping Over a Nested Dictionary

python
users = {"sam": {"age": 28}, "alex": {"age": 30}}
for name, info in users.items():  # outer loop over each user
    print(name, info["age"])  # info is the nested dict for that user

Nested Dictionaries में Shallow Copy का Trap

.copy() (या dict()) से किसी outer dictionary को copy करने पर सिर्फ top level copy होता है — इसके अंदर की कोई भी nested dictionary original और copy के बीच shared reference रहती है, यानी copy के through nested value में बदलाव original को भी असर करता है। genuinely independent copy के लिए copy.deepcopy() चाहिए।

Note: copy.deepcopy() specifically तब इस्तेमाल कीजिए जब आपको नेस्टेड dictionary structure को original से पूरी तरह independent बनाना हो, सिर्फ उसके top level को नहीं।
Warning: किसी shallow-copied dictionary के through nested value को modify करने पर चुपचाप original भी बदल जाती है, क्योंकि nested dictionary कभी असल में duplicate ही नहीं हुई थी।

उदाहरण: The Shallow Copy Trap with Nested Dictionaries

python
import copy
original = {"user": {"age": 28}}
shallow = original.copy()
shallow["user"]["age"] = 99
print(original["user"]["age"])  # also changed

deep = copy.deepcopy(original)
deep["user"]["age"] = 1
print(original["user"]["age"])  # unaffected
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.