Praful Patel
Praful Patel

Reputation: 105

Scala: Converting 2D list into 1D

I have a scala list

val x = List[(a,b), (c,d), (d,e)]

I want to convert above 2D list into 1D.

expected output val x = List(a,b,c,d,d,e)

I have tried using "x.flatten", but it didn't work.

How can I convert 2D list into 1D.

Upvotes: 2

Views: 541

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51683

Do

List((a, b), (c, d), (d, e)).map { case (x, y) => List(x, y) }.flatten

or

List((a, b), (c, d), (d, e)).flatMap { case (x, y) => List(x, y) }

Upvotes: 1

Related Questions