1

Basically i want to split a list into multiple lists

For example:

If I input:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

Would split and return

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23]]

Another example:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Would return

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]

And another one:

[0, 1, 2, 3, 4, 5, 6]

Would return

[[0, 1, 2, 3, 4, 5, 6]]

Here i made each split to be 10 items each. So if the total item in the list is not divisible by 10, the last list would just have less than 10 items

I've been thinking of a way to do this for a while, but the way i was thinking on how to do this is kinda long and probably is not a very good way to do it.

So what would be a good way to do this?

tzaman
  • 46,925
  • 11
  • 90
  • 115
M Isa
  • 33
  • 5

0 Answers0