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 cummulative 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 Cummulative 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 cummulative using accumulate and join() is used to construct the resultant List of lists.
The original list is : ['gfg-is-all-best'] The Cummulative List Splits : ['gfg', 'gfg-is', 'gfg-is-all', 'gfg-is-all-best']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course