I have a query
which is a list of numbers. I want to get the ranges of indices in which the number 1
appears. The range starts when 1
appears and ends on the index in which it doesn't appear. I have made an example to illustrate this.
query= [0,0,0,0,0,1,1,1,0,1,0,0,1,1,0]
answer = [[5,8],[9,10],[12,14]]
Note: I am not looking for the first and last index of some value in a list in Python. I'm looking for all the places in which they start and end.
Update: From some of the suggested answers below it looks like Itertools is quite handy for this stuff.