← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 10 of 15

Python Comprehensions Advanced

Advanced comprehensions एक ही line में lists, sets और dictionaries बनाते हैं, यहाँ तक कि loops के अंदर loops के साथ भी। यह एक compact recipe जैसा है जो एक साथ पूरा batch बना देता है।
Syntax
python
[expression for item in iterable if condition]
[expression for outer in iter1 for inner in iter2]
{expression for item in iterable}

Advanced List Comprehensions

एक single list comprehension एक ही expression में कई for clauses (nested loops के लिए) और कई if clauses (compound conditions के लिए) मिला सकता है, जो अन्यथा nested loops और conditionals की कई lines की जगह एक ज़्यादा compact, single-line equivalent देता है।

उदाहरण: Advanced List Comprehensions

python
matrix = [[1, 2], [3, 4]]
result = [x for row in matrix for x in row if x % 2 == 0]  # nested loop plus a filter, all in one expression
print(result)

Dictionary Comprehensions

Dictionary comprehensions list comprehensions जैसा ही for/if structure इस्तेमाल करते हैं लेकिन {key_expr: value_expr for ...} syntax से key-value pairs बनाते हैं, जिससे आप किसी existing iterable से एक ही expression में dynamically dictionary बना सकते हैं, न कि manually loop करके keys assign करके।

उदाहरण: Dictionary Comprehensions

python
nums = [1, 2, 3]
squares = {n: n * n for n in nums}  # builds key-value pairs in one expression
print(squares)

Set Comprehensions

Set comprehensions {expr for ...} syntax इस्तेमाल करते हैं (कोई colon नहीं, जो इन्हें dict comprehensions से अलग करता है) और duplicate results को automatically discard कर देते हैं, क्योंकि sets परिभाषा से सिर्फ unique elements रखते हैं -- यह तब उपयोगी है जब आपको किसी बड़े dataset से derived distinct values चाहिए हों।

उदाहरण: Set Comprehensions

python
nums = [1, 2, 2, 3, 3]
unique_squares = {n * n for n in nums}  # {} with no colon means a set, duplicates dropped automatically
print(unique_squares)

Nested Comprehensions

एक comprehension को दूसरे के अंदर nest करना, जैसे एक list comprehension जो एक और list comprehension की हर row बनाता है, आपको matrices या grids जैसी 2D structures एक compact expression में generate करने देता है, हालाँकि nesting दो levels से ज़्यादा गहरी होते ही readability जल्दी बिगड़ने लगती है।

उदाहरण: Nested Comprehensions

python
matrix = [[row * 3 + col for col in range(3)] for row in range(2)]
print(matrix)

Memory और Generator Expressions

सभी comprehensions अपना पूरा result एक साथ memory में बनाते हैं, जो सिर्फ एक बार iterate करने वाले बहुत बड़े datasets के लिए wasteful हो जाता है।

Square brackets को parentheses से बदलना किसी list comprehension को generator expression में बदल देता है, जो पूरी collection को memory में रखने की बजाय values को lazily, एक-एक करके compute और yield करता है।

उदाहरण: Memory and Generator Expressions

python
squares_list = [x * x for x in range(5)]  # builds the whole list in memory immediately
squares_gen = (x * x for x in range(5))  # parentheses make this a lazy generator instead
print(squares_list)
print(list(squares_gen))  # values are only computed when consumed
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.