This question already has an answer here:
Ive got a list which actually hold indices for another list. Hence I want to pick out the consecutive numbers from this list
index_list=[3,4,8,9,35,36,37]
from which i want the output as
[3:4], [8:9], [35:37]
---------------------MOTIVE:---------------------
I have another master list of words, which has 80 words.
master_list=['was,'it','to,'go,'I'.........]
Thus the consecutive indices will help me pick out the required words from master_list as
master_list[3:4], master_list[8:9], master_list[35:37]
Using much generalized way:
Update:
There are likely lots of ways to do this. Here's one based on
reduce()
andislice()
:OUTPUT
Note that this treats the second number in the slice in the customary Python manner in that it's one beyond what we want. If it's really the last item of what you want then modify this element with a
+ 1
: