Split list into multiple lists with fixed number of elements

How to split a List of elements into lists with at most N items?

ex: Given a list with 7 elements, create groups of 4, leaving the last group possibly with less elements.

split(List(1,2,3,4,5,6,"seven"),4)

=> List(List(1,2,3,4), List(5,6,"seven"))

I think you're looking for grouped. It returns an iterator, but you can convert the result to a list,

scala> List(1,2,3,4,5,6,"seven").grouped(4).toList
res0: List[List[Any]] = List(List(1, 2, 3, 4), List(5, 6, seven))

There is much easier way to do the task using sliding method. It works this way:

val numbers = List(1, 2, 3, 4, 5, 6 ,7)

Lets say you want to break the list into smaller lists of size 3.

numbers.sliding(3, 3).toList

will give you

List(List(1, 2, 3), List(4, 5, 6), List(7))