The splitting of lists is quite common utility nowadays and there can be many applications and use cases of the same. Along with these always come the variations. One such variation can be split the list by particular value. Let’s discuss a certain way in which list split can be performed.
Method : Using list comprehension +
zip() + slicing + enumerate()
This problem can be solved in two parts, in first part we get the index list by which split has to be performed using enumerate function. And then we can join the values according to the indices using zip and list slicing.
The original list : [1, 4, 5, 6, 4, 5, 6, 5, 4] The list after splitting by a value : [[1, 4, 5], [6, 4, 5], [6, 5], ]
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.