Take the 2-minute tour ×
val xsa = List(1,2,3)
val xsb = List(1,2,3)
for (i <- 0 until xsa.size) {
    val a = xsa(i)
    val b = xsb(i)
    println(a + "_" + b)

I want to simplify the length equals' List or Array. I hoped to do it like this:

for (a <- xsa; b <- xsb)
    println(a + "_" + b)

But why does it only iterate xsb once?

share|improve this question
add comment

3 Answers

up vote 18 down vote accepted
for ((a,b) <- xsa zip xsb)
  println(a + "_" + b)
share|improve this answer
add comment
(xsa, xsb).zipped foreach ((a, b) => println (a + "_" + b))


(xsa, xsb).zipped map (_ + "_" + _) foreach println
share|improve this answer
good for point out zipped method, thx –  Googol Shan Dec 30 '10 at 14:45
add comment

Try Seq.zip

val x = xsa.zip(xsb)
for ((a, b) <- x)
    println(a + "_" + b)
share|improve this answer
add comment

Your Answer


