Sometimes, while working with python, we can have a problem of splitting a list. This problem is quite common and has many variations. Having solutions to popular variations proves to be good in long run. Let’s discuss certain way to split list in uneven groups as defined by other list.
Method : Using
itertools.islice() + list comprehension
The combination of above functionalities can be used to perform this task. In this,
islice() is used to perform the core task of slicing the list and list comprehension is used to perform the task of binding together logic and iterations. The container is converted to iterator for faster iteration.
The original list is : [1, 4, 5, 7, 6, 5, 4, 2, 10] The split list is : [3, 4, 2] The resultant split list is : [[1, 4, 5], [7, 6, 5, 4], [2, 10]]
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.