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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.