2

I have list of lists like this:

List(List(1, 2, 3), List(1, 2), List(1))

I want to get

List(List(1, 2, 3), List(1, 2), List(1), List()) 

or

List(List(), List(1, 2, 3), List(1, 2), List(1))

Standart concatenation didn't work, so what should I do?

1
  • Hi if the answer below worked could you accept it. Solved questions help SO users focus on older unsolved questions. Commented May 14, 2013 at 6:32

3 Answers 3

1
val l = List(List(1, 2, 3), List(1, 2), List(1))
l: List[List[Int]] = List(List(1, 2, 3), List(1, 2), List(1))

List()+:l
res0: List[List[Int]] = List(List(), List(1, 2, 3), List(1, 2), List(1))
Sign up to request clarification or add additional context in comments.

Comments

0

This works for me.

scala> val ls = List(List(1, 2, 3), List(1, 2), List(1))
ls: List[List[Int]] = List(List(1, 2, 3), List(1, 2), List(1))
scala> val newLs = List()::ls
newLs: List[List[Int]] = List(List(), List(1, 2, 3), List(1, 2), List(1))

1 Comment

@qwwdfsad the append operator is :+
0
ls::List()

is wrong syntax

List()::ls 

works but you have to assign it to a new List. You can't modify the original ls because it is immutable.

Comments

Your Answer

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