Skip to content

Instantly share code, notes, and snippets.

@Kush1101
Last active September 24, 2020 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kush1101/07db8c8627142fecd6d82362eecc88d1 to your computer and use it in GitHub Desktop.
Save Kush1101/07db8c8627142fecd6d82362eecc88d1 to your computer and use it in GitHub Desktop.
from itertools import accumulate
from operator import mul
iterable = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list(accumulate(iterable)))
# Prints the addition after each iteration
# OUTPUT
"""
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
"""
print(list(accumulate(iterable, mul)))
# Prints the mutiplication after each iteration (= factorial of the number)
"""
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment