0

I need to generate a list of lists in that special way:

[3, 1, 4] -> [[1, 2, 3], [1], [1, 2, 3, 4]]

That means that every list in a list of lists must be in range of the given list values. I've tried smth like:

L = [3, 1, 4]
q = [i for i in L]
print(list([x] for x in range(y for y in q)))

But it return a TypeError: generator cannot be interpreted as an integer

That all has to be a single generator expression.

1 Answer 1

0

Using a list comprehension.

Try:

L = [3, 1, 4]
print([list(range(1, i+1)) for i in L])

Output:

[[1, 2, 3], [1], [1, 2, 3, 4]]
Sign up to request clarification or add additional context in comments.

2 Comments

That works! Thank you a lot. Could you please explain me the construction of list(range())? (Just a little bit)
In python3 range is a generator. that is why I am encapsulating it in list. if I do not do that you will get a generator expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.