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

Python Dict Comprehension

Dictionary comprehension एक line में पूरा labeled collection बनाती है, जैसे हर guest के लिए एक साथ name tags बनाना। यह key-value pairs बनाने का एक compact तरीका है।
Syntax
python
new_dict = {key_expr: value_expr for item in iterable if condition}

Basic Dict Comprehension

Dictionary comprehensions {key_expr: value_expr for item in iterable} का इस्तेमाल करके एक expression में नई dict बनाती हैं, जो list comprehension का dict equivalent है और आमतौर पर एक खाली dict के साथ loop करके बार-बार assign करने से ज़्यादा तेज़ और पठनीय होती है।

उदाहरण: Basic Dict Comprehension

python
nums = [1, 2, 3]
squares = {n: n * n for n in nums}  # key is n, value is its square
print(squares)

If-Conditions से Filter करना

अंत में 'if condition' जोड़ने से यह filter होता है कि कौन-से source items entries बनेंगे — {k: v for k, v in pairs if v > 0} सिर्फ वे pairs रखता है जिनकी value positive है, बाकी को transform करने की बजाय पूरी तरह discard कर देता है।

उदाहरण: Filtering with If-Conditions

python
pairs = {"a": 1, "b": -2, "c": 3}
positive = {k: v for k, v in pairs.items() if v > 0}  # if filters out non-positive values
print(positive)

Keys और Values को Transform करना

आप key और value दोनों को एक ही source item से compute कर सकते हैं, जिससे data को साथ-साथ reshape किया जा सके — उदाहरण के लिए {name.lower(): len(name) for name in names} key को normalize भी करता है और एक ही pass में नई value भी derive करता है।

उदाहरण: Transforming Keys and Values

python
names = ["Alice", "Bob"]
lengths = {name.lower(): len(name) for name in names}  # key is lowercased, value is the length
print(lengths)

दो Lists से Dict बनाना

zip() दो समान-लंबाई वाली lists को element-by-element pair करता है, और इसे comprehension में wrap करने पर — {k: v for k, v in zip(keys, values)} — दो parallel lists बिना manual loop के एक dictionary बन जाती हैं।

उदाहरण: Creating Dict from Two Lists

python
keys = ["a", "b", "c"]
values = [1, 2, 3]
combined = {k: v for k, v in zip(keys, values)}  # zip pairs the two lists element by element
print(combined)

If-Else Conditions के साथ Comprehension

for clause से पहले ternary if-else रखने पर हर key को बाहर निकालने की बजाय condition के आधार पर एक value मिलती है — {k: pass if v >= 50 else fail for k, v in scores.items()} हर key रखता है पर उसकी mapped value बदल देता है।

उदाहरण: Comprehension with If-Else Conditions

python
scores = {"Alex": 80, "Sam": 40}
results = {k: "pass" if v >= 50 else "fail" for k, v in scores.items()}  # every key is kept, value depends on the condition
print(results)
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.