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]]
- Python | Split tuple into groups of n
- Python | Split string in groups of n consecutive characters
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Zip Uneven Tuple
- Python | Find groups of strictly increasing numbers in a list
- Python | Split list into lists by particular value
- Python | Custom list split
- Python | Uneven Sized Matrix Column product
- Python | Column summation in uneven sized lists
- Python - Uneven Sized Matrix Column Minimum
- Python - Split String of list on K character
- Python | Split a list having single integer
- Python - Split heterogeneous type list
- Python | Split string into list of characters
- Python | Split a list into sublists of given lengths
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.