I'm looking for a function that takes in a list, and iterates through every possible range for example:
example = [1,2,3,4]
for i in a_list[i:j ]:
# do something
iterates through ranges between 0:j
- [1,2]
- [1,2,3]
- [1,2,3,4]
and then it iterates through ranges between 1:j
- [2,3]
- [2,3,4]
and then it iterates through ranges between 2:j
- [3,4]
And so on...
CodePudding user response:
Use nested loops:
>>> example = [1,2,3,4]
>>> for i in range(len(example)):
... for j in range(i, len(example)):
... print(example[i:j 1])
...
[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[2]
[2, 3]
[2, 3, 4]
[3]
[3, 4]
[4]