Python – Cumulative List Split
Sometimes, while working with String lists, we can have a problem in which we need to perform the task of split and return all the split instances of list in cumulative way. This kind of problem can occur in many domains in which data is involved. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is brute force way in which this task is performed. In this, we test for the list and append the list when ever we encounter the split character.
The original list is : ['gfg-is-all-best'] The Cumulative List Splits : [['gfg'], ['gfg-is'], ['gfg-is-all'], ['gfg-is-all-best']]
Method #2 : Using accumulate() + join()
This is one-liner approach to this problem. In this, we perform the task of cutting into cumulative using accumulate and join() is used to construct the resultant List of lists.
The original list is : ['gfg-is-all-best'] The Cumulative List Splits : ['gfg', 'gfg-is', 'gfg-is-all', 'gfg-is-all-best']