7

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

Srinivas
  • 553
  • 9
  • 21

3 Answers3

14

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))

flavian
  • 28,161
  • 11
  • 65
  • 105
Keith Pinson
  • 1,725
  • 1
  • 14
  • 17
12

Should be easy to achieve:

(a zip b) zip c map {
    case ((x, y), z) => (x, y, z)
};
flavian
  • 28,161
  • 11
  • 65
  • 105
6

Use:

(a zip b) zip c map { case ((av,bv),cv) => (av,bv,cv) }

Note: This shortens the result list of the shortest of a,b,c. If you'd rather have the result list padded with default values, use zipAll.

gzm0
  • 14,752
  • 1
  • 36
  • 64