Can I zip more than two lists together in Scala?
Asked Answered
R

12

106

Given the following Scala List:

val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3"))

How can I get:

List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3"))

Since zip can only be used to combine two Lists, I think you would need to iterate/reduce the main List somehow. Not surprisingly, the following doesn't work:

scala> l reduceLeft ((a, b) => a zip b)
<console>:6: error: type mismatch;
 found   : List[(String, String)]
 required: List[String]
       l reduceLeft ((a, b) => a zip b)

Any suggestions one how to do this? I think I'm missing a very simple way to do it.

Update: I'm looking for a solution that can take a List of N Lists with M elements each and create a List of M TupleNs.

Update 2: As it turns out it is better for my specific use-case to have a list of lists, rather than a list of tuples, so I am accepting pumpkin's response. It is also the simplest, as it uses a native method.

Redeem answered 2/11, 2009 at 23:49 Comment(3)
possible duplicate of Zip multiple sequencesInsulate
Definitely worth noting: #1683812Aeroballistics
@VenkatSudheerReddyAedama Also asked by me, five days later. ;-)Redeem
E
38

I don't believe it's possible to generate a list of tuples of arbitrary size, but the transpose function does exactly what you need if you don't mind getting a list of lists instead.

Eucken answered 3/11, 2009 at 1:16 Comment(3)
Thanks, that works perfectly! As I go into my specific use case, I see that a list of lists would be better anyway, as I need to map and reduce the various sub-lists.Redeem
@JoshCason in the narrowest sense of "more than two", sure. Three is indeed more than two. I interpreted the question in the broader sense of "more than two", meaning arbitrarily many. And in that case, it is not possible to do what the question wants, unless you reach for HLists and the like.Eucken
the link in the answer is broken, new link is scala-lang.org/api/2.12.1/scala/…Powel
O
232
scala> (List(1,2,3),List(4,5,6),List(7,8,9)).zipped.toList
res0: List[(Int, Int, Int)] = List((1,4,7), (2,5,8), (3,6,9))

For future reference.

Otey answered 11/9, 2011 at 6:20 Comment(3)
This is great for zipping three lists. Shame this doesn't work for more than three list :(Crumpler
Do note that this needs to be in a tuple first: zipped is not a function of List.Attis
zipped is deprecated in Scala 2.13. in 2.13, do l1.lazyZip(l2).lazyZip(l3).toListRootless
E
38

I don't believe it's possible to generate a list of tuples of arbitrary size, but the transpose function does exactly what you need if you don't mind getting a list of lists instead.

Eucken answered 3/11, 2009 at 1:16 Comment(3)
Thanks, that works perfectly! As I go into my specific use case, I see that a list of lists would be better anyway, as I need to map and reduce the various sub-lists.Redeem
@JoshCason in the narrowest sense of "more than two", sure. Three is indeed more than two. I interpreted the question in the broader sense of "more than two", meaning arbitrarily many. And in that case, it is not possible to do what the question wants, unless you reach for HLists and the like.Eucken
the link in the answer is broken, new link is scala-lang.org/api/2.12.1/scala/…Powel
C
35

So this piece of code won't answer the needs of the OP, and not only because this is a four year old thread, but it does answer the title question, and perhaps someone may even find it useful.

To zip 3 collections:

as zip bs zip cs map { 
  case ((a,b), c) => (a,b,c)
}
Celibacy answered 18/12, 2013 at 5:17 Comment(3)
to do 4 collections looks like: as zip bs zip cs zip ds map { case ((a,b),c)} map {case ((a,b),c,d)=>(a,b,c,d)}Housebreak
@JamesTobin, u shorten to as zip bs zip cs zip ds map {case (((a,b),c),d)=>(a,b,c,d) }Harberd
Nice for lists of varying type.Procrastinate
C
11

Yes, with zip3.

Corum answered 3/11, 2009 at 0:6 Comment(2)
Thanks, but it only works with 3 lists. I'm looking for a solution that can take a List of N Lists with M elements each and create a List of M TupleNs.Redeem
The link is brokenTips
L
7

transpose does the trick. A possible algorithm is:

def combineLists[A](ss:List[A]*) = {
    val sa = ss.reverse;
    (sa.head.map(List(_)) /: sa.tail)(_.zip(_).map(p=>p._2 :: p._1))
}

For example:

combineLists(List(1, 2, 3), List(10,20), List(100, 200, 300))
// => List[List[Int]] = List(List(1, 10, 100), List(2, 20, 200))

The answer is truncated to the size of the shortest list in the input.

combineLists(List(1, 2, 3), List(10,20))
// => List[List[Int]] = List(List(1, 10), List(2, 20))
Lempres answered 29/10, 2012 at 18:34 Comment(2)
this answer almost does the trick, however, it reverse the elements. Can you suggest an improved version that produces the output in the expected order? thanksReginaldreginauld
Modified version that retains the order: def combineLists[A](ss:List[A]*) = { val sa = ss.reverse; (sa.head.map(List(_)) /: sa.tail)(_.zip(_).map(p=>p._2 :: p._1)) }Milagro
C
6

If you don't want to go down the applicative scalaz/cats/(insert your favourite functional lib here) route, pattern matching is the way to go, although the (_, _) syntax is a bit awkward with nesting, so let's change it:

import scala.{Tuple2 => &}

for (i1 & i2 & i3 & i4 <- list1 zip list2 zip list3 zip list4) yield (i1, i2, i3, i4)

The & is an arbitrary choice here, anything that looks nice infix should do it. You'll likely get a few raised eyebrows during code review, though.

It should also work with anything you can zip (e.g. Futures)

Ceilometer answered 2/9, 2016 at 9:47 Comment(0)
S
5

Scala treats all of its different tuple sizes as different classes (Tuple1, Tuple2, Tuple3, Tuple4,...,Tuple22) while they do all inherit from the Product trait, that trait doesn't carry enough information to actually use the data values from the different sizes of tuples if they could all be returned by the same function. (And scala's generics aren't powerful enough to handle this case either.)

Your best bet is to write overloads of the zip function for all 22 Tuple sizes. A code generator would probably help you with this.

Sosthena answered 3/11, 2009 at 1:10 Comment(0)
C
5

I don't believe that's possible without being repetitive. For one simple reason: you can't define the returning type of the function you are asking for.

For instance, if your input was List(List(1,2), List(3,4)), then the return type would be List[Tuple2[Int]]. If it had three elements, the return type would be List[Tuple3[Int]], and so on.

You could return List[AnyRef], or even List[Product], and then make a bunch of cases, one for each condition.

As for general List transposition, this works:

def transpose[T](l: List[List[T]]): List[List[T]] = l match {
  case Nil => Nil
  case Nil :: _ => Nil
  case _ => (l map (_.head)) :: transpose(l map (_.tail))
}
Chewning answered 3/11, 2009 at 2:3 Comment(3)
This won't work for arbitrary sized lists. For example: transpose(List(List("a", "b"), List("c")))Aeroballistics
@VenkatSudheerReddyAedama Transposition of incomplete matrices doesn't make sense to me. To take your example, if c in line with a or with b? And how would you represent it being in line with the other?Chewning
Agreed. That's an incomplete matrix. I was looking for something along the lines of zipAll. Say in my case, c is in line with a (i.e., in-line with index) ?Aeroballistics
N
4

Scala 2.12.13 and below

If you know how long the input List is, you can join the list into a Tuple and use Tuple's .zipped method:

val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3"))

println(l match {
  case l1::l2::l3::_ => (l1,l2,l3).zipped.toList
  case _ => throw new IllegalArgumentException("List is not the right length")
}) // List((a1,a2,a3), (b1,b2,b3), (c1,c2,c3))

Scastie Example - 2.12.13

>= Scala 2.13

The above solution is deprecated - use lazyZip instead:

val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3"))

println(l match {
  case l1::l2::l3::_ => (l1 lazyZip l2 lazyZip l3).toList
  case _ => throw new IllegalArgumentException("List is not the right length")
}) // List((a1,a2,a3), (b1,b2,b3), (c1,c2,c3))

Scastie Example - 2.13.0

Nitrogenize answered 1/3, 2021 at 10:47 Comment(0)
P
2

product-collections has aflatZip operation up to arity 22.

scala> List(1,2,3) flatZip Seq("a","b","c") flatZip Vector(1.0,2.0,3.0) flatZip Seq(9,8,7)
res1: com.github.marklister.collections.immutable.CollSeq4[Int,String,Double,Int] = 
CollSeq((1,a,1.0,9),
        (2,b,2.0,8),
        (3,c,3.0,7))
Perpetua answered 24/2, 2014 at 22:20 Comment(0)
S
0

With Scalaz:

import scalaz.Zip
import scalaz.std.list._

// Zip 3
Zip[List].ap.tuple3(List("a1", "b1"),
                    List("a2", "b2"),
                    List("a3", "b3"))

// Zip 4
Zip[List].ap.tuple4(List("a1", "b1"),
                    List("a2", "b2"),
                    List("a3", "b3"),
                    List("a4", "b4"))

// Zip 5
Zip[List].ap.tuple5(List("a1", "b1"),
                    List("a2", "b2"),
                    List("a3", "b3"),
                    List("a4", "b4"),
                    List("a5", "b5"))

For more than 5:

// Zip 6
Zip[List].ap.apply6(List("a1", "b1"),
                    List("a2", "b2"),
                    List("a3", "b3"),
                    List("a4", "b4"),
                    List("a5", "b5"),
                    List("a6", "b6"))((_, _, _, _, _, _))

// Zip 7
Zip[List].ap.apply7(List("a1", "b1"),
                    List("a2", "b2"),
                    List("a3", "b3"),
                    List("a4", "b4"),
                    List("a5", "b5"),
                    List("a6", "b6"),
                    List("a7", "b7"))((_, _, _, _, _, _, _))

...

// Zip 12
Zip[List].ap.apply12(List("a1", "b1"),
                     List("a2", "b2"),
                     List("a3", "b3"),
                     List("a4", "b4"),
                     List("a5", "b5"),
                     List("a6", "b6"),
                     List("a7", "b7"),
                     List("a8", "b8"),
                     List("a9", "b9"),
                     List("a10", "b10"),
                     List("a11", "b11"),
                     List("a12", "b12"))((_, _, _, _, _, _, _, _, _, _, _, _))
Sinuous answered 11/12, 2014 at 9:26 Comment(0)
Q
0

Suppose we have three lists with names a, b, and c. mydata is in form of (a, (b, c)):

val mydata = a.zip(b.zip(c)).toList

then newly created result is in form of (a, b, c):

val result = for (x, (y, z)) <- mydata yield (x, y, z)

for example:

scala> val a = (1, 2, 3)
val a: (Int, Int, Int) = (1,2,3)
                                                                                                    
scala> val b = (4, 5, 6)
val b: (Int, Int, Int) = (4,5,6)
                                                                                                    
scala> val c = (7, 8, 9)
val c: (Int, Int, Int) = (7,8,9)
                                                                                                    
scala> val mydata = a.zip(b.zip(c)).toList
val mydata:
  List[(Int, (Int, Int)) | ((Int, (Int, Int)) | ((Int, (Int, Int)) | Nothing))] = List((1,(4,7)), (2,(5,8)), (3,(6,9)))

scala> val result = for (x, (y, z)) <- mydata yield (x, y, z)
val result: List[(Int, Int, Int)] = List((1,4,7), (2,5,8), (3,6,9))

scala> result.foreach(println)
(1,4,7)
(2,5,8)
(3,6,9)
Query answered 27/7, 2023 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.