Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Itertools.chain()
Next article icon

Itertools.accumulate()-Python

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be empty. Example:

Python
import itertools  a = [1, 2, 3, 4] print(list(itertools.accumulate(a))) 

Output
[1, 3, 6, 10] 

Explanation: accumulate() function iterates through the list, maintaining a running total. It starts with 1, adds 2 to get 3, adds 3 to get 6 and adds 4 to get 10, producing the cumulative sums.

Syntax of itertools.accumulate()

itertools.accumulate(iterable, func=None)

Parameters:

  • iterable: The input sequence (like a list, tuple, or set).
  • func (optional): A binary function (e.g., operator.add, operator.mul, max, etc.). If not provided, addition is used by default.

Returns: An iterator that yields accumulated results.

Examples of itertools.accumulate()

Example 1: In this example, we are using itertools.accumulate() along with operator.mul to compute the cumulative product of the elements in the list.

Python
import itertools import operator  a = [1, 2, 3, 4, 5] res = itertools.accumulate(a, operator.mul) print(list(res)) 

Output
[1, 2, 6, 24, 120] 

Explanation: Multiplication is applied from left to right. First, it starts with 1, then 1 * 2 = 2, then 2 * 3 = 6, followed by 6 * 4 = 24 and finally 24 * 5 = 120.

Example 2: In this example, we are using itertools.accumulate() with the built-in max function to compute the running maximum of the elements in the list.

Python
import itertools  a = [5, 3, 6, 2, 1, 9, 1] res = itertools.accumulate(a, max) print(list(res)) 

Output
[5, 5, 6, 6, 6, 9, 9] 

Explanation: Maximum value is tracked as we move through the list. It starts with 5. Then max(5, 3) = 5, max(5, 6) = 6, max(6, 2) = 6, max(6, 1) = 6, max(6, 9) = 9 and max(9, 1) = 9.

Example 3: In this example, we are using itertools.accumulate() with a custom lambda function lambda x, y: x - y to compute a running subtraction.

Python
import itertools  a = [1, 2, 3, 4] res = itertools.accumulate(a, lambda x, y: x - y) print(list(res)) 

Output
[1, -1, -4, -8] 

Explanation: Subtraction is applied from left to right using a custom lambda function. It starts with 1, then 1 - 2 = -1, then -1 - 3 = -4, and finally -4 - 4 = -8.

Example 4: In this example, itertools.accumulate() is used to compute the cumulative sum of the elements in the set difference b.difference(a).

Python
import itertools a = {5, 3, 6, 2, 1, 9} b = {4, 2, 6, 0, 7}  diff = b.difference(a)  res = itertools.accumulate(diff) print(list(res)) 

Output
[0, 4, 11] 

Explanation: b.difference(a) gives the set of elements in b but not in a, which is {0, 4, 7} . Then addition is applied.Start with 0, then 0 + 4 = 4 and 4 + 7 = 11.


Next Article
Python - Itertools.chain()

Y

YashKhandelwal8
Improve
Article Tags :
  • Python
  • Python-itertools
Practice Tags :
  • python

Similar Reads

    Python Itertools
    Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and we wa
    12 min read
    Python - Itertools.count()
    Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it co
    3 min read
    Python - Itertools.cycle()
    Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators
    3 min read
    Python - itertools.repeat()
    Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
    2 min read
    Itertools.accumulate()-Python
    itertools.accumulate() is an iterator that takes two arguments, an iterable (target) and an optional function. The function is applied at each iteration to accumulate the result. By default, if no function is provided, it performs addition. If the input iterable is empty, the output will also be emp
    3 min read
    Python - Itertools.chain()
    The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is
    4 min read
    Python - Itertools.chain.from_iterable()
    Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Th
    2 min read
    Python - Itertools.compress()
    Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Co
    2 min read
    Python - Itertools.dropwhile()
    Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more inform
    1 min read
    Python - Itertools.filterfalse()
    In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() funct
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences