If you can endure the call to get the length of the list, then
l.grouped( (l.length+2)/3 ).toList
will create something similar to what you want (if val l = List(34, 11, 23, 1, 9, 83, 5) , then you will get List(List(34, 11, 23), List(1, 9, 83), List(5)) back, but if you want a roughly equal distribution across your lists, then you will need to create your own method for this - there is no library function that splits the list into equal parts by n .
Something like this will work if you want to keep order in order:
def chopList[T]( l: List[T], pieces: Int, len: Int = -1, done: Int = 0, waiting: List[List[T]]=Nil ): List[List[T]] = { if (l isEmpty) waiting.reverse else { val n = (if (len<0) l.length else len) val ls = l.splitAt( (n.toLong*(done+1)/pieces - n.toLong*done/pieces).toInt ) chopList(ls._2,pieces,n,done+1,ls._1 :: waiting) } }
and this will happen to return exactly what you want: List(List(34, 11), List(23, 1), List(9, 83, 5)) .
If you do not want to request the length of the list, then you can write a method that creates a bunch of buckets and introduces a new element dropwise each time.