I need to create a list of lists from a range:
for example range(0,10) -->[[0,5],[6,10]]
I think possible to do it using list comprehension but cannot figure out a way for the same
I tried this list_com = [[x, x + 5] for x in range(0, 10, 5)]
but it gives [[0, 5], [5, 10]]
and if I try, list_com = [[x + 1, x + 5] for x in range(0, 10, 5)]
I get [[1, 5], [6, 10]]
which is close except for first list, where I want [[0,5],[6,10]]
Is there a better way to do it or fix the above to remove the first list bug.
The first sub-element differs from the others in that it has an inclusive range of 6 whereas all others have a range of 5.
You can eliminate the complexity of a conditional test in a list comprehension by constructing the first sub-element separately as follows:
def genElements(g=5, n=200):
yield [0, g]
for i in range(g, n, g):
yield [i+1, i+g]
l = list(genElements())
print(l)