Python Itertools Accumulate














































Python Itertools Accumulate



Python itertools Module : accumulate Function

Accumulate function in itertools module accumulates the iterables based on the function(func) provided to it as an argument. If func is not provided to it then by default it adds the iterables. The function(func) provided to this function should accept two variables and return the result. Accumulate function returns an iterator to the output, so to see all outputs we need to iterate over it. Elements of the input iterable may be any type that can be accepted as arguments to func. If the input iterable is empty, the output iterable will also be empty. There are a number of uses for the func argument. It can be set to min() for a running minimum, max() for a running maximum, or operator.mul() for a running product.


Syntax

itertools.accumulate(iterable[,func])


Parameters

iterable : Required. Elements to be accumulated.

func : Optional. Function to accumulate iterables.


Sample Program

#importing itertools module
import itertools iterables = [1,3,6,2,7,9,3,1,11] data = itertools.accumulate(iterables) print(list(data)) iterables = [1,3,6,2,7,8,3,1,11]
data = itertools.accumulate(iterables)
print(list(data)) iterables = [1,3,6,2,7,9,3,1,11] data = itertools.accumulate(iterables,lambda x,y : x*y) print(list(data)) def collect(x,y):
z = y.lower()
return x+z+z iterables = ['A','B','C','D'
] data = itertools.accumulate(iterables,collect)


Output

[1, 4, 10, 12, 19, 28, 31, 32, 43]

[1, 3, 18, 36, 252, 2268, 6804, 6804, 74844]

['A', 'Abb', 'Abbcc', 'Abbccdd']


----------------------------------------* END *----------------------------------------


More Articles of Deekshant Wadhwa:

Name Views Likes
Python Secrets token_urlsafe 1375 1
Python Bisect Module 527 1
Python Base64 Decodebytes 466 1
Python Copy copy 428 1
Python JSON Module 430 1
Python Copy Module 408 1
Python Base64 Encode 527 1
Python Base64 urlsafe_b64decode 571 1
Python Copy deepcopy 440 1
Python 3.9 New removeprefix() and removesuffix() string methods 952 1
Python 3.9 Dictionary Merge & Update Operators 648 1
Python Base64 Decode 441 1
Python Secrets compare_digest 1732 1
Python Itertools Chain 544 1
Python Itertools Permutations 1242 1
Python Itertools Module 425 1
Python Base64 b85decode 619 1
Python Secrets token_bytes 789 1
Python ModuleNotFoundError: No module named cv2 744 1
Python Base64 standard_b64encode 497 1
Python Itertools Count 688 1
Python Secrets Module 669 1
Python Itertools Groupby 446 1
Python Base64 b32decode 709 1
Python Itertools Filterfalse 677 1
Python Itertools Product 471 1
Python Base64 Encodebytes 497 1
Python Itertools Cycle 474 2
Python Base64 b64decode 459 1
Python Itertools Starmap 600 1
Python Itertools combinations_with_replacement 502 1
Python Base64 urlsafe_b64encode 564 1
Python Itertools Repeat 507 1
Python Base64 standard_b64decode 478 1
Python Base64 b85encode 479 1
Python Itertools Islice 574 1
Python Bisect insort 585 1
Python Itertools Combinations 670 1
Python Secrets token_hex 792 1
Python Itertools Compress 494 1
Python Itertools Tee 527 1
Python Base64 b64encode 423 1
Python Itertools zip_longest 922 1
Python Itertools Accumulate 1452 1
Python Itertools Dropwhile 543 1
Python Secrets Randbits 465 1
Python Base64 Module 432 1
Python Itertools combinations_with_replacement 371 10
Python Secrets Choice 450 1
Python Secrets Randbelow 438 1
Python Itertools Takewhile 498 1
Python Bisect bisect 376 1
Python Base64 b32encode 557 1

Comments