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 | Column summation in uneven sized lists
- Python | Find groups of strictly increasing numbers in a list
- Python | Custom list split
- Python | Split list into lists by particular value
- Python | Split nested list into two lists
- Python | Split a list having single integer
- Python - Split heterogeneous type list
- Python | Split a list into sublists of given lengths
- Python | Split string into list of characters
- Python | Split strings and digits from string list
- Python | Split dictionary of lists to list of dictionaries
- Python | Split strings in list with same prefix in all elements
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.