Python | Split list into lists by particular value

The splitting of lists is quite common utility nowadays and there can be many applications and use cases of the same. Along with these always come the variations. One such variation can be split the list by particular value. Let’s discuss a certain way in which list split can be performed.

Method : Using list comprehension + zip() + slicing + enumerate()

This problem can be solved in two parts, in first part we get the index list by which split has to be performed using enumerate function. And then we can join the values according to the indices using zip and list slicing.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# Split list into lists by particular value
# Using list comprehension + zip() + slicing + enumerate()
  
# initializing list
test_list = [1, 4, 5, 6, 4, 5, 6, 5, 4]
  
# printing original list
print("The original list : " + str(test_list))
  
# using list comprehension + zip() + slicing + enumerate()
# Split list into lists by particular value
size = len(test_list)
idx_list = [idx + 1 for idx, val in
            enumerate(test_list) if val == 5]
  
  
res = [test_list[i: j] for i, j in
        zip([0] + idx_list, idx_list + 
        ([size] if idx_list[-1] != size else []))]
  
# print result
print("The list after splitting by a value : " + str(res))

chevron_right


Output :

The original list : [1, 4, 5, 6, 4, 5, 6, 5, 4]
The list after splitting by a value : [[1, 4, 5], [6, 4, 5], [6, 5], [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.