Python | Custom list split
Development and sometimes machine learning applications require splitting lists into smaller list in a custom way, i.e on certain values on which split has to be performed. This is quite a useful utility to have knowledge about. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + zip()
By coupling the power of list comprehension and zip(), this task can be achieved. In this, we zip beginning and end of list and then keep slicing the list as they arrive and cutting off new lists from them.
# Python3 code to demonstrate # to perform custom list split # using list comprehension + zip() # initializing string test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4] # initializing split index list split_list = [2, 5, 7] # printing original list print ("The original list is : " + str(test_list)) # printing original split index list print ("The original split index list : " + str(split_list)) # using list comprehension + zip() # to perform custom list split res = [test_list[i : j] for i, j in zip([0] + split_list, split_list + [None])] # printing result print ("The splitted lists are : " + str(res)) |
The original list is : [1, 4, 5, 6, 7, 3, 5, 9, 2, 4] The original split index list : [2, 5, 7] The splitted lists are : [[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]
Method #2 : Using itertools.chain() + zip()
The task performed by the list comprehension function of getting the split chunks can also be done using chain function. This is more useful when we wish to handle larger lists as this method is more efficient.
# Python3 code to demonstrate # to perform custom list split # using itertools.chain() + zip() from itertools import chain # initializing string test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4] # initializing split index list split_list = [2, 5, 7] # printing original list print ("The original list is : " + str(test_list)) # printing original split index list print ("The original split index list : " + str(split_list)) # using itertools.chain() + zip() # to perform custom list split temp = zip(chain([0], split_list), chain(split_list, [None])) res = list(test_list[i : j] for i, j in temp) # printing result print ("The splitted lists are : " + str(res)) |
The original list is : [1, 4, 5, 6, 7, 3, 5, 9, 2, 4] The original split index list : [2, 5, 7] The splitted lists are : [[1, 4], [5, 6, 7], [3, 5], [9, 2, 4]]
Recommended Posts:
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Custom Cycle list
- Python | Custom slicing in List
- Python | Custom Multiplication in list of lists
- Python | Split list into lists by particular value
- Python | Split a list having single integer
- Python | Split list in uneven groups
- Python | Split a list into sublists of given lengths
- Python | Split string into list of characters
- Python | Split nested list into two lists
- Python | Split dictionary of lists to list of dictionaries
- Python | Split strings in list with same prefix in all elements
- Python | Split and Pass list as separate parameter
- Python | Split given list and insert in excel file
- Python | Split list of strings into sublists based on length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



