Home › Hindi › Dictionary Hindi में
Python dictionary Hindi में — get, loop, nested और KeyError से बचाव
🌐 यह page Hindi में है · Read this in English →
हर method का outputKeyError से बचने के 3 तरीकेFree browser practice
Dictionary क्या है — एक line में
List में items का पता number (index) होता है। Dictionary में पता आप खुद चुनते हो — उसे key कहते हैं। इसलिए इसे "नाम से ढूँढने वाली list" समझ सकते हो।
marks = {"Hindi": 85, "Maths": 92, "Science": 78}
print(marks["Maths"]) # 92
print(len(marks)) # 3
print("Hindi" in marks) # True — key ढूँढता है, value नहीं
Key हमेशा immutable होनी चाहिए — string, number या tuple चलेगी। list को key बनाओगे तो TypeError: unhashable type: 'list' आएगा। Value कुछ भी हो सकती है।
KeyError — और उससे बचने के 3 पक्के तरीके
marks = {"Hindi": 85}
print(marks["English"])
KeyError: 'English'
square brackets वाला तरीका key न मिलने पर program तोड़ देता है। तीन safe तरीके:
# 1. get() — न मिले तो None, crash नहीं
print(marks.get("English")) # None
print(marks.get("English", 0)) # 0 (अपना default)
# 2. पहले पूछ लो
if "English" in marks:
print(marks["English"])
# 3. try / except
try:
print(marks["English"])
except KeyError:
print("English के marks नहीं हैं")
रोज़ के code में get() ही सबसे साफ है। setdefault() एक कदम आगे जाता है — key न हो तो बना भी देता है: marks.setdefault("English", 0)।
जोड़ना, बदलना, हटाना
marks = {"Hindi": 85}
marks["Maths"] = 92 # नई key जुड़ी
marks["Hindi"] = 90 # पुरानी बदली (same syntax!)
marks.update({"Science": 78, "Hindi": 95}) # एक साथ कई
del marks["Science"] # key न हो तो KeyError
x = marks.pop("Maths") # x = 92, key हट गई
y = marks.pop("Urdu", 0) # y = 0, कोई error नहीं
ध्यान दो — नई key बनाने और पुरानी बदलने का syntax एक ही है। इसीलिए typo से नई key बन जाती है और पुरानी अछूती रह जाती है; यह चुपचाप होने वाला bug है, error नहीं आता।
Dictionary पर loop चलाना
marks = {"Hindi": 85, "Maths": 92}
for subject in marks: # default में keys मिलती हैं
print(subject) # Hindi / Maths
for subject in marks.keys():
print(subject, marks[subject])
for score in marks.values():
print(score) # 85 / 92
for subject, score in marks.items(): # ✅ सबसे काम का
print(subject, "=", score)
items() सबसे ज़्यादा use होता है क्योंकि key और value दोनों एक साथ मिल जाती हैं।
चेतावनी: जिस dictionary पर loop चल रहा है उसमें key जोड़ना या हटाना मत — RuntimeError: dictionary changed size during iteration आएगा। बदलाव करना हो तो for k in list(marks): लिखो।
Nested dictionary — असली data ऐसा ही दिखता है
students = {
"Riya": {"Hindi": 85, "Maths": 92},
"Aman": {"Hindi": 70, "Maths": 65},
}
print(students["Riya"]["Maths"]) # 92
print(students.get("Sara", {}).get("Maths")) # None — safe
for naam, subjects in students.items():
total = sum(subjects.values())
print(naam, "total:", total)
JSON और API से आने वाला data हमेशा ऐसा ही nested होता है। Nested में get() की chain (.get("Sara", {})) crash से बचाने का standard तरीका है।
काम की 5 बातें जो exam में पूछी जाती हैं
- Keys unique होती हैं।
{"a": 1, "a": 2}का नतीजा{'a': 2}है — बाद वाली जीतती है। - Python 3.7+ में order बना रहता है — जिस क्रम में डाला, उसी क्रम में निकलेगा।
- Dictionary comprehension:
{k: v*2 for k, v in marks.items()} - sorted() dict पर keys की list देता है:
sorted(marks)→['Hindi', 'Maths'] - खाली dict falsy है:
if not marks:का मतलब "dictionary खाली है"।
Value के हिसाब से sort करना हो: sorted(marks.items(), key=lambda x: x[1], reverse=True) — topper निकालने का यही एक-line तरीका है।
🛠️ Practice करो (free, browser में)
पढ़ना काफी नहीं — यही bug खुद fix करके देखो। नीचे के problems browser में ही चलते हैं, कोई install नहीं, कोई login नहीं।
- गायब key access करना — classic KeyError
- get() with default — crash के बिना value
- न होने वाली key delete करना — del बनाम pop
- नई key जोड़ने का गलत syntax — 2 मिनट में fix
- Nested dictionary access — JSON वाला पैटर्न
- Keys unique होती हैं — duplicate key का नतीजा
अक्सर पूछे जाने वाले सवाल
Python dictionary में KeyError कैसे रोकें?
marks["key"] की जगह marks.get("key") use करो — key न मिले तो None मिलेगा, crash नहीं। अपना default भी दे सकते हो: marks.get("key", 0)। या पहले if "key" in marks: से जाँच लो।
dictionary की key list क्यों नहीं बन सकती?
Key hashable यानी immutable होनी चाहिए ताकि उसका hash कभी न बदले। list बदल सकती है इसलिए TypeError: unhashable type: 'list' आता है। उसी data का tuple बनाकर key बना सकते हो।
keys(), values() और items() में क्या फर्क है?
keys() सिर्फ keys देता है, values() सिर्फ values, और items() दोनों की जोड़ी देता है। loop में सबसे ज़्यादा items() काम आता है क्योंकि एक ही बार में key और value मिल जाती हैं।
dictionary को value के हिसाब से कैसे sort करें?
sorted(marks.items(), key=lambda x: x[1], reverse=True) लिखो। यह (key, value) जोड़ियों की list देता है जो value के घटते क्रम में होती है — topper निकालने का सबसे छोटा तरीका।