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

Python reduce()

reduce() पूरी list को items को दो-दो करके combine करके एक value में समेट देता है, बिल्कुल एक लंबे कागज़ को मोड़कर छोटा वर्ग बनाने जैसा। सारे numbers जोड़ना इसका एक आम इस्तेमाल है।
Syntax
python
from functools import reduce

reduce(function, iterable, initial_value)

reduce() क्या है?

reduce(function, iterable), एक two-argument function को running total और अगले element पर बार-बार, बाएँ से दाएँ, लागू करके sequence को एक single accumulated value में समेट देता है।

map() और filter() के विपरीत, यह built-in नहीं है -- इसे functools module से import करना ज़रूरी है।

उदाहरण: What is reduce()?

python
from functools import reduce
numbers = [1, 2, 3, 4]
total = reduce(lambda acc, x: acc + x, numbers)  # folds the list into one accumulated value
print(total)

reduce() से Lists जोड़ना

reduce(lambda acc, x: acc + x, numbers) से किसी list को जोड़ना यह pattern साफ दिखाता है: accumulator पहले element से शुरू होता है, और हर step पर अगला number उसमें जुड़ता जाता है, अंत में total बनता है -- हालाँकि इस specific case के लिए Python का built-in sum() यही काम कहीं ज़्यादा सीधे तरीके से कर देता है।

उदाहरण: Summing Lists with reduce()

python
from functools import reduce
numbers = [1, 2, 3, 4]
total = reduce(lambda acc, x: acc + x, numbers)  # accumulator pattern
print(total)
print(sum(numbers))  # built-in sum() does the same thing more directly

Maximum ढूँढना

वही accumulator pattern running accumulator की हर अगले element से तुलना करके और जो बड़ा (या छोटा) हो उसे रखते हुए maximum या minimum ढूँढता है, जो अनिवार्य रूप से यही है कि reduce() किसी भी pairwise comparison को एक single sequence-wide result में कैसे generalize करता है।

उदाहरण: Finding the Maximum

python
from functools import reduce
numbers = [3, 7, 2, 9, 4]
maximum = reduce(lambda acc, x: x if x > acc else acc, numbers)  # keeps the larger of acc and x each step
print(maximum)

reduce() से Strings जोड़ना

reduce() सिर्फ numbers तक सीमित नहीं है -- एक string-concatenation lambda पास करने से strings की एक list एक combined string में समेट जाती है, जो दिखाता है कि accumulator pattern हर उस type के लिए काम करता है जो आपके दिए combining operation को support करता है।

उदाहरण: Concatenating Strings with reduce()

python
from functools import reduce
words = ["Hello", " ", "World"]
sentence = reduce(lambda acc, w: acc + w, words)  # accumulator pattern works for strings too
print(sentence)

Initializer Values का इस्तेमाल

reduce() को तीसरा argument पास करने से accumulator के लिए sequence के पहले element का इस्तेमाल करने के बजाय एक explicit initial value set हो जाती है।

यह दोनों वजहों से मायने रखता है: एक खाली sequence के साथ correctness के लिए (जो वरना TypeError raise करती), और उन मामलों के लिए जहाँ आप चाहते हैं कि fold किसी specific baseline value से शुरू हो।

उदाहरण: Using Initializer Values

python
from functools import reduce
numbers = []
total = reduce(lambda acc, x: acc + x, numbers, 0)  # explicit initial value, avoids TypeError on an empty list
print(total)
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.