I would like to consecutively group a list using the elements that appear in another list.
For example,
x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
y = [2,3,1,5,6,4]
I would like to group list x
using list y
such that a new list, say list z
, would read:
z = [[0,1],[2,3,4],[5],[6,7,8,9,10],[11,12,13,14,15,16],[17,18,19,20]]
There ought to be a pythonic way of doing this, but im having trouble coding it.
Also, I have looked around the internet for different methods of grouping lists but none of methods I have found help me group lists using a grouping value that changes per group (like in the example above).
Can anyone help?