0

Given a base list myList and a list of the desired lengths of each sub-list lengths

myList = [0,1,2,3,4,5,6,7,8,9]
lengths = [3,4,3]

How would the following list of lists be created in Python?

result = [[0,1,2],[3,4,5,6],[7,8,9]]

The sum of each integer in lengths will always be equal to the length of myList, no end effects!

B Flandard
  • 21
  • 2
  • 1
    What have you tried so far? This is not a service to write code for you – SpaceKatt Feb 25 '21 at 23:32
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. – Prune Feb 25 '21 at 23:36
  • 1
    if you want a one liner (destroying `myList`): `[ [ myList.pop(0) for i in range(l) ] for l in lengths ]` – halloleo Feb 26 '21 at 02:59

0 Answers0