Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala, converting multiple lists to list of tuples [duplicate]

Tags:

scala

I have 3 lists like

val a = List("a", "b", "c")
val b = List(1, 2, 3)
val c = List(4, 5, 6)

I want convert them as follows

List(("a", 1, 4), ("b", 2, 5), ("c", 3, 6))

Please let me know how to get this result

like image 697
Srinivas Avatar asked May 17 '13 17:05

Srinivas


2 Answers

If you have two or three lists that you need zipped together you can use zipped

val a = List("a", "b", "c")
val b = List(1, 2, 3)
val c = List(4, 5, 6) 

(a,b,c).zipped.toList

This results in: List((a,1,4), (b,2,5), (c,3,6))

like image 171
Keith Pinson Avatar answered Oct 22 '22 13:10

Keith Pinson


Should be easy to achieve:

(a zip b) zip c map {
    case ((x, y), z) => (x, y, z)
};
like image 12
flavian Avatar answered Oct 22 '22 12:10

flavian