Python Comprehensions Advanced
In this page:
[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
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
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
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
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
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
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects