I know how to split a list into even groups, but I'm having trouble splitting it into uneven groups.
Essentially here is what I have: some list, let's call it mylist
, that contains x elements.
I also have another file, lets call it second_list, that looks something like this:
{2, 4, 5, 9, etc.}
Now what I want to do is divide mylist
into uneven groups by the spacing in second_list. So, I want my first group to be the first 2 elements of mylist
, the second group to be the next 4 elements of mylist
, the third group to be the next 5 elements of mylist
, the fourth group to be the next 9 elements of `mylist, and so on.
Is there some easy way to do this? I tried doing something similar to if you want to split it into even groups:
for j in range(0, len(second_list)):
for i in range(0, len(mylist), second_list[j]):
chunk_mylist = mylist[i:i+second_list[j]]
However this doesn't split it like I want it to. I want to end up with my # of sublists being len(second_list)
, and also split correctly, and this gives a lot more than that (and also splits incorrectly).