# Reduce () in Python

Python Methods and Functions

Works:

• The first step selects the first two elements of the sequence and produces the result.
• The next step is to apply the same function to earlier the result, and the number just follows the second element and the result is saved again.
• This process continues until there are no more elements left in the container.
• The final result is returned and printed on the console.

 ` # Python code to demonstrate how the limit () method works `   ` # import functools for the limit () method ` ` import ` ` functools `   ` # initializing list ` ` lis ` ` = ` ` [` ` 1 ` `, ` ` 3 ` `, ` ` 5 ` `, ` ` 6 ` `, ` ` 2 ` `,] `   ` # using the method Reduce the sum of the list ` ` print ` ` (` ` "The sum of the list elements is:" ` `, end ` ` = ` ` " ") ` ` print ` ` (functools. ` ` reduce ` ` (` ` lambda ` ` a, b: a ` ` + ` ` b, lis)) ``   # using Reduce to compute the maximum item from the list print ( "The maximum element of the list is:" , end = "") print (functools. reduce ( lambda a, b: a if a & gt; b else b, lis)) `

Output:

` The sum of the list elements is: 17 The maximum element of the list is: 6 `

Using operator functions

Reduce () can also be combined with operator functions to achieve lambda-like functionality and make the code more readable.

 ` # Python code to demonstrate how the limit () method works ` ` # using operator functions `   ` # import functools for the limit () method ` ` import functools ``   # import statement for operator functions import operator   # initializing list lis = [ 1 , 3 , 5 , 6 , 2 ,]   # using the Reduce method to compute the list sum # using operator functions print ( "The sum of the list elements is:" , end = "") print (functools. reduce (operator.add, lis))   # using Reduce to compute the product # using operator functions print ( "The product of list elements is: " , end = " ") print (functools. reduce (operator.mul, lis))   # concatenate the string using a reducer print ( "The concatenated product is:" , end = " ") print (functools. reduce (operator.add, [ " geeks " , "for" , "geeks" ])) `

Exit

` The sum of the list elements is: 17 The product of list elements is: 180 The concatenated product is: pythonengineering `

decrease () against accumulate ()

Both Reduce () and accumulate () can be used for you numeric summation of the elements of the sequence. But there are implementation differences in both of them.

• Reduce () is defined in the functools module, accumulated () in the itertools module.
• Reduce () stores the intermediate result and returns only the final summation value. Whereas, the accumulation () method returns a list containing intermediate results. The last number of the returned list is the sum of the list.
• Reduce (Fun, Seq) takes a function as its first and a sequence as its second argument. Unlike accumulation (seq, fun) takes a sequence as 1st argument and a function as 2nd argument.

` `

` #code Python to demonstrate summation # using Reduce () and accumulate ()   # imports itertools to accumulate () import itertools   # import functools for the limit () method import functools   # initializing list lis = [ 1 , 3 , 4 , 10 , 4 ]   # summing printing using accumulation () print ( "The summation of list using accumulate is:" , end = "") print ( list (itertools.accumulate (lis, lambda x, y: x + y)))   < code class = "comments"> # summarize printing using Redu () print ( "The summation of list using reduce is:" , end = "") print (functools. reduce ( lambda x, y: x + y, lis)) `

` `

Output:

` The summation of list using accumulate is: [1, 4, 8, 18, 22] The summation of list using reduce is: 22 `

This article courtesy of Manjeet Singh (S. Nandini) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8951406,"math_prob":0.7829445,"size":1983,"snap":"2021-43-2021-49","text_gpt3_token_len":445,"char_repetition_ratio":0.10763012,"word_repetition_ratio":0.036474165,"special_character_ratio":0.23550177,"punctuation_ratio":0.1336761,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99581754,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-06T15:00:26Z\",\"WARC-Record-ID\":\"<urn:uuid:3ca98bce-595b-4929-b0e8-7b14530682b2>\",\"Content-Length\":\"64784\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0ec9520d-030b-4101-8b12-d774b09a5d19>\",\"WARC-Concurrent-To\":\"<urn:uuid:86a90068-6e72-4e5c-9ecb-26a3a6e67922>\",\"WARC-IP-Address\":\"172.67.181.212\",\"WARC-Target-URI\":\"https://python.engineering/reduce-in-python/\",\"WARC-Payload-Digest\":\"sha1:3EE4EF5BH7XDBCFD3VIVP2DMPITEJLHX\",\"WARC-Block-Digest\":\"sha1:6BEYXLZCJJGYPQKFKI7FBQGBZDXHZDI2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363301.3_warc_CC-MAIN-20211206133552-20211206163552-00436.warc.gz\"}"}