UGC-NET, GATE, NIELIT (O/A/B Level), KVS/NVS Computer Teacher, PGDCA, BCA, MCA, B-Tech व All India IT Exam विशेष नोट्स: इस अध्याय में हम Functional Programming in Python: lambda, map, filter and reduce (पायथन में फंक्शनल प्रोग्रामिंग: lambda, map, filter और reduce) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और राष्ट्रीय स्तर की परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Python supports functional programming idioms through first-class functions, anonymous lambda expressions, and higher-order functions like map, filter, and reduce. Understanding lazy evaluation of map/filter iterators, functools.reduce, and when list comprehensions are more Pythonic is essential for clean Python code.
अध्याय सार: पायथन फर्स्ट-क्लास फंक्शन्स, एनोनिमस lambda एक्सप्रेशन और map, filter तथा reduce जैसे हायर-ऑर्डर फंक्शन्स के माध्यम से फंक्शनल प्रोग्रामिंग का समर्थन करता है। map/filter के लेजी इटरेटर्स, functools.reduce और लिस्ट कॉम्प्रिहेंशन के सही उपयोग को समझना स्वच्छ पायथन कोड के लिए महत्वपूर्ण है।
Functional Programming in Python: lambda, map, filter and reduce: Detailed Notes & Architecture (पायथन में फंक्शनल प्रोग्रामिंग: lambda, map, filter और reduce)
1. Anonymous Functions with lambda (lambda के साथ एनोनिमस फंक्शन्स)
[EN] A lambda expression creates a small, single-expression anonymous function using `lambda args: expression` without needing a `def` name. Lambdas cannot contain statements (like `return`, `for`, `assert`, or assignments `=`), and are commonly used as inline sort keys (`key=lambda x: x[1]`).
[HI] lambda एक्सप्रेशन `lambda args: expression` सिंटैक्स का उपयोग करके बिना `def` नाम के एक छोटा सिंगल-एक्सप्रेशन एनोनिमस फंक्शन बनाता है। इसमें स्टेटमेंट्स (जैसे `return`, `for`, `=`) नहीं हो सकते और इसका उपयोग अक्सर सॉर्ट की (`key=lambda x: x[1]`) में होता है।
2. map() and Lazy Iteration in Python 3 (पायथन 3 में map() और लेजी इटरेशन)
[EN] `map(func, iterable, ...)` applies `func` to every item of one or more iterables in parallel, stopping at the shortest iterable. In Python 3, `map()` returns a lazy `map` iterator (unlike Python 2's list), computing values on demand via `next()` or `list()`.
[HI] `map(func, iterable, ...)` एक या अधिक इटरेबल्स के प्रत्येक तत्व पर `func` लागू करता है और सबसे छोटे इटरेबल पर रुकता है। पायथन 3 में `map()` लिस्ट के बजाय एक लेजी इटरटर लौटाता है जो `list()` या `next()` पर मान निकालता है।
3. filter() and Truthy Filtering (filter() और ट्रूथी फिल्टरिंग)
[EN] `filter(func, iterable)` yields only elements for which `func(item)` returns a truthy value, also returning a lazy iterator in Python 3. Passing `None` as the first argument (`filter(None, items)`) removes all falsy elements (`0`, `None`, `""`, `[]`, `False`).
[HI] `filter(func, iterable)` केवल उन्हीं तत्वों को लौटाता है जिनके लिए `func(item)` सत्य (truthy) मान देता है। पहले आर्ग्युमेंट के रूप में `None` देने (`filter(None, items)`) पर सभी falsy मान (`0`, `None`, `""`, `False`) हट जाते हैं।
4. functools.reduce() and List Comprehensions (functools.reduce() और लिस्ट कॉम्प्रिहेंशन)
[EN] Moved from built-ins to `functools.reduce(func, iterable[, initializer])` in Python 3, `reduce` cumulatively folds items left-to-right into a single value. While `map`/`filter` are great with existing functions, Pythonic style (PEP 8 / Guido) often prefers list comprehensions `[x*2 for x in nums if x > 0]` for readability.
[HI] पायथन 3 में `reduce` को बिल्ट-इन से हटाकर `functools.reduce` में रखा गया है, जो तत्वों को बाएं से दाएं जोड़कर एक एकल मान बनाता है। पठनीयता के लिए पायथन में अक्सर `map`/`filter` की जगह लिस्ट कॉम्प्रिहेंशन को प्राथमिकता दी जाती है।
Syntax, Formula & Technical Example (Functional Programming in Python: lambda, map, filter and reduce)
from functools import reduce
nums = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x ** 2, nums)) # [1, 4, 9, 16, 25]
evens = list(filter(lambda x: x % 2 == 0, nums)) # [2, 4]
product = reduce(lambda a, b: a * b, nums, 1) # 120
Functional Programming in Python: lambda, map, filter and reduce Quick Reference & Comparison Table
| Construct | Module / Return Type (Py3) | Primary Purpose |
|---|---|---|
| lambda | Built-in keyword / function object | Single-expression inline anonymous function |
| map() | Built-in / lazy map iterator | Transform every item across 1+ iterables |
| filter() | Built-in / lazy filter iterator | Keep items where predicate is truthy |
| reduce() | functools module / single scalar | Cumulatively fold iterable left-to-right |
Functional Programming in Python: lambda, map, filter and reduce Top 20 Bilingual MCQs with Answers
Q1. In Python 3, what data type does `map(lambda x: x*2, [1, 2, 3])` return directly?
(पायथन 3 में `map(lambda x: x*2, [1, 2, 3])` सीधे किस डेटा टाइप को लौटाता है?)
(A) A set {2, 4, 6}
(B) A tuple (2, 4, 6)
(C) A lazy map iterator object
(D) A Python list [2, 4, 6]
Correct Answer: (C)
[EN] Unlike Python 2 (which returned a list), Python 3's map() and filter() return memory-efficient lazy iterators.
[HI] पायथन 3 में map() और filter() सीधे लिस्ट के बजाय मेमोरी-कुशल लेजी इटरटर लौटाते हैं।
Q2. In Python 3, from which standard module must `reduce()` be imported?
(पायथन 3 में `reduce()` को किस मानक मॉड्यूल से इम्पोर्ट करना पड़ता है?)
(A) itertools
(B) functools
(C) collections
(D) operator
Correct Answer: (B)
[EN] Guido van Rossum moved reduce() out of built-ins into the functools module in Python 3.
[HI] पायथन 3 में reduce() को बिल्ट-इन से हटाकर functools मॉड्यूल में रखा गया है।
Q3. What is the output of `list(filter(None, [0, 1, '', 'AI', None, [], True]))`?
(`list(filter(None, [0, 1, '', 'AI', None, [], True]))` का आउटपुट क्या होगा?)
(A) [0, '', None, []]
(B) [1, 'AI', True]
(C) TypeError
(D) [1, 'AI']
Correct Answer: (B)
[EN] Passing None to filter() acts as the identity truthiness check, keeping only truthy items.
[HI] filter() में None पास करने पर केवल truthy मान ([1, 'AI', True]) बचते हैं और सभी falsy मान हट जाते हैं।
Q4. Which of the following is NOT allowed inside a Python `lambda` expression body?
(पायथन `lambda` एक्सप्रेशन बॉडी के अंदर निम्नलिखित में से क्या अनुमत नहीं है?)
(A) Calling another function
(B) Multiple parameters with default values
(C) Ternary conditional expressions (a if cond else b)
(D) Statements like `return`, `for` loops, or `=` assignments
Correct Answer: (D)
[EN] Python lambdas are restricted to a single expression; statements (except walrus `:=` in 3.8+) raise SyntaxError.
[HI] पायथन lambda केवल एक एक्सप्रेशन तक सीमित है; इसमें return या for लूप जैसे स्टेटमेंट नहीं लिखे जा सकते।
Q5. What does `reduce(lambda a, b: a + b, [10, 20, 30], 5)` evaluate to?
(`reduce(lambda a, b: a + b, [10, 20, 30], 5)` का परिणाम क्या होगा?)
(A) 65
(B) 35
(C) 60
(D) 5
Correct Answer: (A)
[EN] The optional third argument (5) is the initializer: (((5 + 10) + 20) + 30) = 65.
[HI] तीसरा आर्ग्युमेंट (5) इनिशियलाइज़र है: (((5 + 10) + 20) + 30) = 65।
Q6. What happens when `map()` is given two lists of unequal lengths: `list(map(lambda x, y: x + y, [1, 2, 3], [10, 20]))`?
(जब `map()` को असमान लंबाई की दो लिस्ट दी जाएं: `list(map(lambda x, y: x + y, [1, 2, 3], [10, 20]))`, तो क्या होगा?)
(A) It raises IndexError
(B) It stops at the shortest iterable and returns [11, 22]
(C) It pads with 0 and returns [11, 22, 3]
(D) It pads the shorter list with None
Correct Answer: (B)
[EN] Just like zip(), map() terminates as soon as the shortest input iterable is exhausted.
[HI] zip() की तरह, map() सबसे छोटे इटरेबल के समाप्त होते ही रुक जाता है और [11, 22] लौटाता है।
Q7. What happens if you call `list(m)` a second time on the same `m = map(str, [1, 2])` iterator?
(यदि आप एक ही `m = map(str, [1, 2])` इटरटर पर दूसरी बार `list(m)` कॉल करें तो क्या होगा?)
(A) It returns ['1', '2'] again
(B) It returns an empty list [] because the iterator is exhausted
(C) It doubles the elements
(D) It raises StopIteration
Correct Answer: (B)
[EN] Iterators are single-pass; once consumed by the first list(m), subsequent iterations yield nothing.
[HI] इटरटर केवल एक बार चलता है; पहली बार उपभोग होने के बाद दूसरी बार खाली लिस्ट [] मिलती है।
Q8. Which list comprehension is functionally equivalent to `list(map(lambda x: x*x, filter(lambda x: x>0, nums)))`?
(कौन सा लिस्ट कॉम्प्रिहेंशन `list(map(lambda x: x*x, filter(lambda x: x>0, nums)))` के समतुल्य है?)
(A) [x * x if x > 0 for x in nums]
(B) (x * x for x in nums)
(C) [x > 0 for x in nums if x * x]
(D) [x * x for x in nums if x > 0]
Correct Answer: (D)
[EN] List comprehensions combine mapping and filtering cleanly in a single readable expression.
[HI] [x * x for x in nums if x > 0] एक ही पठनीय एक्सप्रेशन में मैपिंग और फिल्टरिंग दोनों करता है।
Q9. What does `reduce(lambda a, b: a + b, [])` do when given an empty list and NO initializer?
(बिना इनिशियलाइज़र के खाली लिस्ट देने पर `reduce(lambda a, b: a + b, [])` क्या करता है?)
(A) Returns 0
(B) Returns None
(C) Raises TypeError
(D) Returns []
Correct Answer: (C)
[EN] Without an initializer, reduce() has no initial value to return for an empty sequence and raises TypeError.
[HI] बिना इनिशियलाइज़र के खाली सीक्वेंस पर reduce() TypeError उत्पन्न करता है।
Q10. In Python sorting `sorted(users, key=lambda u: u['age'])`, what is the role of the lambda?
(`sorted(users, key=lambda u: u['age'])` में lambda की क्या भूमिका है?)
(A) It sorts in descending order automatically
(B) It extracts the comparison key from each dictionary before sorting
(C) It filters out users without age
(D) It modifies each dictionary in-place
Correct Answer: (B)
[EN] The key function is called once per element to compute its sort comparison value.
[HI] key फंक्शन सॉर्ट करने से पहले प्रत्येक एलिमेंट से तुलना के लिए मान ('age') निकालता है।
Q11. Which `functools` function creates a new function with some arguments pre-filled (partial application)?
(कौन सा `functools` फंक्शन कुछ आर्ग्युमेंट्स को पहले से भरकर एक नया फंक्शन बनाता है?)
(A) functools.cmp_to_key
(B) functools.wraps
(C) functools.partial
(D) functools.lru_cache
Correct Answer: (C)
[EN] functools.partial(func, *args, **kwargs) freezes a portion of a function's arguments.
[HI] functools.partial किसी फंक्शन के कुछ आर्ग्युमेंट्स को फिक्स करके नया फंक्शन बनाता है।
Q12. What does 'First-Class Functions' mean in Python?
(पायथन में 'फर्स्ट-क्लास फंक्शन्स' (First-Class Functions) का क्या अर्थ है?)
(A) Functions cannot have default arguments
(B) Functions always execute faster than loops
(C) Functions can only be defined at the top of a file
(D) Functions can be assigned to variables, passed as arguments, and returned from other functions
Correct Answer: (D)
[EN] In Python, functions are objects, enabling higher-order functions like map, filter, and decorators.
[HI] पायथन में फंक्शन्स ऑब्जेक्ट्स होते हैं जिन्हें वेरिएबल में रखा जा सकता है और दूसरे फंक्शन में पास या रिटर्न किया जा सकता है।
Q13. What is the output of `(lambda x, y=10: x + y)(5)`?
(`(lambda x, y=10: x + y)(5)` का आउटपुट क्या होगा?)
(A) 10
(B) 15
(C) 5
(D) TypeError
Correct Answer: (B)
[EN] This immediately invoked lambda uses x=5 and default y=10, returning 15.
[HI] यह तुरंत कॉल किया गया lambda x=5 और डिफ़ॉल्ट y=10 का उपयोग करके 15 लौटाता है।
Q14. Which `itertools` function returns all intermediate accumulated results instead of only the final scalar like `reduce()`?
(कौन सा `itertools` फंक्शन `reduce()` की तरह केवल अंतिम मान के बजाय सभी मध्यवर्ती संचित परिणाम लौटाता है?)
(A) itertools.accumulate
(B) itertools.product
(C) itertools.islice
(D) itertools.chain
Correct Answer: (A)
[EN] itertools.accumulate([1, 2, 3, 4]) yields [1, 3, 6, 10], whereas reduce yields 10.
[HI] itertools.accumulate सभी मध्यवर्ती संचित मानों का इटरटर लौटाता है।
Q15. Why is `(x * 2 for x in range(1000000))` more memory-efficient than `[x * 2 for x in range(1000000)]`?
(`[x * 2 for x in range(1000000)]` की तुलना में `(x * 2 for x in range(1000000))` अधिक मेमोरी-कुशल क्यों है?)
(A) Tuples are compressed in RAM
(B) Generators skip odd numbers
(C) Parentheses create a lazy generator expression that yields one item at a time
(D) Parentheses run in C++ directly
Correct Answer: (C)
[EN] Generator expressions compute values lazily on demand in O(1) memory, unlike lists which materialize all items in RAM.
[HI] पैरेंथेसिस एक लेजी जनरेटर एक्सप्रेशन बनाता है जो पूरी लिस्ट RAM में बनाने के बजाय एक समय में एक ही मान उत्पन्न करता है।
Q16. Which of the following statements about functional programming in Python is NOT true?
(पायथन में फंक्शनल प्रोग्रामिंग के बारे में निम्नलिखित में से कौन सा कथन सत्य नहीं है?)
(A) `lambda` functions can contain multiple `if/elif/else` blocks and `for` loops
(B) reduce() is located in the functools module in Python 3
(C) Functions are first-class objects in Python
(D) map() and filter() return lazy iterators in Python 3
Correct Answer: (A)
[EN] Python lambdas only allow a single expression (such as an inline conditional `a if c else b`), never multi-line loop or block statements.
[HI] पायथन lambda में केवल एक सिंगल एक्सप्रेशन हो सकता है, इसमें मल्टी-लाइन `for` लूप या ब्लॉक स्टेटमेंट नहीं लिखे जा सकते।
Q17. Within Functional Programming in Python: lambda, map, filter and reduce, which construct directly maps to: "Built-in keyword / function object"?
(Functional Programming in Python: lambda, map, filter and reduce में कौन सा Construct सीधे "Built-in keyword / function object" से संबंधित है?)
(A) map()
(B) lambda
(C) filter()
(D) reduce()
Correct Answer: (B)
[EN] In Functional Programming in Python: lambda, map, filter and reduce, 'lambda' handles 'Built-in keyword / function object' (Single-expression inline anonymous function).
[HI] Functional Programming in Python: lambda, map, filter and reduce में 'lambda' का कार्य/विवरण 'Built-in keyword / function object' (Single-expression inline anonymous function) है।
Q18. Which option accurately describes 'map()' in Functional Programming in Python: lambda, map, filter and reduce?
(Functional Programming in Python: lambda, map, filter and reduce के संदर्भ में 'map()' की सही विशेषता कौन सी है?)
(A) Built-in keyword / function object
(B) functools module / single scalar
(C) Built-in / lazy map iterator
(D) Built-in / lazy filter iterator
Correct Answer: (C)
[EN] 'map()' provides 'Built-in / lazy map iterator' — key point: Transform every item across 1+ iterables.
[HI] 'map()' का मुख्य विवरण 'Built-in / lazy map iterator' है (Transform every item across 1+ iterables)।
Q19. In Functional Programming in Python: lambda, map, filter and reduce, identify the correct parameter for the specification: "Built-in / lazy filter iterator".
(Functional Programming in Python: lambda, map, filter and reduce में "Built-in / lazy filter iterator" विवरण किस विकल्प के लिए सही है?)
(A) reduce()
(B) filter()
(C) map()
(D) lambda
Correct Answer: (B)
[EN] 'filter()' corresponds to 'Built-in / lazy filter iterator' (Keep items where predicate is truthy).
[HI] 'filter()' का संबंध 'Built-in / lazy filter iterator' (Keep items where predicate is truthy) से है।
Q20. Considering Functional Programming in Python: lambda, map, filter and reduce, what key behavior defines 'reduce()'?
(Functional Programming in Python: lambda, map, filter and reduce में 'reduce()' की मुख्य भूमिका या मान क्या है?)
(A) Built-in / lazy filter iterator
(B) Built-in / lazy map iterator
(C) functools module / single scalar
(D) Built-in keyword / function object
Correct Answer: (C)
[EN] 'reduce()' represents 'functools module / single scalar' (Cumulatively fold iterable left-to-right).
[HI] 'reduce()' का अर्थ/मान 'functools module / single scalar' है (Cumulatively fold iterable left-to-right)।
Functional Programming in Python: lambda, map, filter and reduce — Exam FAQs
Q1: When should I use `map()`/`filter()` instead of a list comprehension?
मुझे लिस्ट कॉम्प्रिहेंशन के बजाय `map()`/`filter()` का उपयोग कब करना चाहिए?
[EN] Use `map(func, items)` when you already have an existing named function (like `map(int, strings)`) and want lazy evaluation; otherwise, list/generator comprehensions are cleaner than writing a `lambda`.
[HI] जब आपके पास पहले से कोई नामित फंक्शन हो (जैसे `map(int, strings)`) और लेजी इटरेशन चाहिए, तब `map()` बेहतर है; अन्यथा `lambda` के साथ कॉम्प्रिहेंशन अधिक पठनीय होता है।
Q2: Why did Python 3 move `reduce()` into `functools`?
पायथन 3 ने `reduce()` को `functools` में क्यों स्थानांतरित किया?
[EN] Guido van Rossum felt explicit `for` loops or specialized built-ins like `sum()`, `any()`, and `all()` are almost always clearer to read than complex `reduce` lambdas.
[HI] क्योंकि अधिकांश मामलों में जटिल `reduce` lambda की तुलना में `sum()`, `any()`, `all()` या सामान्य `for` लूप अधिक स्पष्ट और पठनीय होते हैं।
Q3: Can a `lambda` function call itself recursively?
क्या कोई `lambda` फंक्शन खुद को रिकर्सिवली कॉल कर सकता है?
[EN] Yes, if assigned to a variable name (e.g., `fact = lambda n: 1 if n <= 1 else n * fact(n - 1)`), though PEP 8 strongly recommends using `def` for named functions.
[HI] हाँ, यदि उसे किसी वेरिएबल में असाइन किया जाए, हालांकि PEP 8 नामित फंक्शन्स के लिए `def` का उपयोग करने की सलाह देता है।
Post a Comment
Post a Comment