{"id":25568,"date":"2021-11-23T08:55:57","date_gmt":"2021-11-23T03:25:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=25568"},"modified":"2021-11-23T08:55:57","modified_gmt":"2021-11-23T03:25:57","slug":"python-itertools-accumulate-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-accumulate-function-with-examples\/","title":{"rendered":"Python Itertools.accumulate() Function with Examples"},"content":{"rendered":"
Itertools Module:<\/strong><\/p>\n The Python itertools module is a collection of tools for working with iterators.<\/p>\n Iterators are simply data types that can be used in a for loop. The list is the most commonly used iterator in Python.<\/p>\n Requirements for using itertools<\/strong><\/p>\n Before using, the itertools module must be imported. Because we want to work with operators, we must also import the operator module.<\/p>\n The Itertools module is a collection of functions.<\/p>\n Itertools.accumulate() Function:<\/strong><\/p>\n This iterator accepts two arguments:<\/p>\n the iterable target and the function to be followed at each iteration of value in target. If no function is specified, addition is performed by default. If the input iterable is empty, so will the output iterable.<\/p>\n This function generates an iterator that iterates through the results of a function.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values<\/strong><\/p>\n iterable:<\/strong> This is Required. Elements to be gathered or accumulated<\/p>\n function:<\/strong> This is Optional. Iterables are accumulated using this function.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n with passing the iterable, function as parameters<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Itertools Module: The Python itertools module is a collection of tools for working with iterators. Iterators are simply data types that can be used in a for loop. The list is the most commonly used iterator in Python. Requirements for using itertools Before using, the itertools module must be imported. Because we want to work …<\/p>\nitertools.accumulate(iterable[, func])<\/pre>\n
Given List = [4, 2, 1, 5, 3]<\/pre>\n
The cumulative of the given list = [4, 6, 7, 12, 15]<\/pre>\n
Given List = [2, 3, 4, 1, 5]\r\nGiven function = lambda a, b: a*b<\/pre>\n
[2, 6, 24, 24, 120]<\/pre>\n
Itertools.accumulate() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [4, 2, 1, 5, 3]\r\n# Pass the given list as an argument to the itertools.accumulate() method that\r\n# calculates cumulative sum of the given list.\r\n# Store it in another variable.\r\nrslt = itertools.accumulate(gvn_lst)\r\n# Convert the above result into a list using the list() function and store\r\n# it in a variable.\r\nrslt_lst = list(rslt)\r\n# Print the cumulative of the given list.\r\nprint(\"The cumulative of the given list =\", rslt_lst)\r\n<\/pre>\n
The cumulative of the given list = [4, 6, 7, 12, 15]<\/pre>\n
import itertools\r\ngvn_lst = [2, 3, 4, 1, 5]\r\nrslt = itertools.accumulate(gvn_lst, lambda a, b: a*b)\r\nprint(list(rslt))\r\n<\/pre>\n
[2, 6, 24, 24, 120]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import itertools module using the import keyword.\r\nimport itertools\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the itertools.accumulate() method that\r\n# calculates cumulative sum of the given list.\r\n# Store it in another variable.\r\nrslt = itertools.accumulate(gvn_lst)\r\n# Convert the above result into a list using the list() function and store\r\n# it in a variable.\r\nrslt_lst = list(rslt)\r\n# Print the cumulative of the given list.\r\nprint(\"The cumulative of the given list =\", rslt_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 50 20 30 10\r\nThe cumulative of the given list = [10, 60, 80, 110, 120]<\/pre>\n