Scala: How to merge two sequential collections into pairs with ‘zip’

This is an excerpt from the 1st Edition of the Scala Cookbook, partially modified for the internet. This is Recipe 10.23, “How to Merge Two Sequential Collections into Pairs with the zip method”

Problem

You want to merge data from two Scala sequential collections into a collection of key/value pairs.

Solution

Use the zip method that’s available on sequential collections to join two sequences into one:

scala> val women = List("Wilma", "Betty")
women: List[String] = List(Wilma, Betty)

scala> val men = List("Fred", "Barney")
men: List[String] = List(Fred, Barney)

scala> val couples = women zip men
couples: List[(String, String)] = List((Wilma,Fred), (Betty,Barney))

This creates an Array of Tuple2 elements, which is a merger of the two original sequences.

This code shows one way to loop over the resulting collection:

scala> for ((wife, husband) <- couples) {
     |     println(s"$wife is married to $husband")
     | }
Wilma is married to Fred
Betty is married to Barney

Once you have a sequence of tuples like couples, you can convert it to a Map, which may be more convenient:

scala> val couplesMap = couples.toMap
couplesMap: scala.collection.immutable.Map[String,String] = Map(Wilma -> Fred, Betty -> Barney)

Discussion

If one collection contains more items than the other collection, the items at the end of the longer collection will be dropped. In the previous example, if the prices collection contained only one element, the resulting collection will contain only one Tuple2:

// three elements
scala> val products = Array("breadsticks", "pizza", "soft drink")
products: Array[String] = Array(breadsticks, pizza, soft drink)

// one element
scala> val prices = Array(4)
prices: Array[Int] = Array(4)

// one resulting element
scala> val productsWithPrice = products.zip(prices)
productsWithPrice: Array[(String, Int)] = Array((breadsticks,4))

Note that the unzip method is the reverse of zip:

scala> val (a,b) = productsWithPrice.unzip
a: collection.mutable.IndexedSeq[String] = ArrayBuffer(breadsticks, pizza, soft drink)
b: collection.mutable.IndexedSeq[Double] = ArrayBuffer(4.0, 10.0, 1.5)

See Also

  • Recipes 10.10, 10.11, and 10.19 demonstrate other uses of the zip method (and zipWithIndex)