Last active
November 24, 2020 14:00
-
-
Save hamdyaea/4e8822f31ba7c28bc8e2be4b2338eaf6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) | |
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]] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment