Scala Tutorial - Learn How To Use Unzip Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 15:13 pm

Overview

In this tutorial, we will learn how to use the unzip function with examples on collection data structures in Scala. The unzip function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The unzip method will unzip and un-merge a collection consisting of element pairs or Tuple2 into two separate collections.

 

As per the Scala documentation, the definition of the unzip method is as follows:

def unzip[A1, A2](implicit asPair: (A) ⇒ (A1, A2)): (CC[A1], CC[A2])

The unzip method is a member of GenericTraversableTemplate trait.

Steps

1. How to initialize a Sequence of donuts

The code below shows how to create a Sequence of donut elements of type String.


println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)

 

2. How to initialize a Sequence of donut prices

Let us create another Immutable Sequence but this time the Sequence will be of the type Double to represent donut prices..


println("\nStep 2: How to initialize a Sequence of donut prices")
val donutPrices = Seq[Double](1.5, 2.0, 2.5)
println(s"Elements of donut prices = $donutPrices")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to initialize a Sequence of donut prices
Elements of donut prices = List(1.5, 2.0, 2.5)

 

3. How to zip the donuts Sequence with their corresponding prices

Using Scala's zip method, you can combine both Sequences from Step 1 and Step 2 to create a single collection containing pairs or Tuple elements. Each tuple will represent a donut along with its corresponding price.


println("\nStep 3: How to zip the donuts Sequence with their corresponding prices")
val zippedDonutsAndPrices: Seq[(String, Double)] = donuts zip donutPrices
println(s"Zipped donuts and prices = $zippedDonutsAndPrices")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to zip the donuts Sequence with their corresponding prices
Zipped donuts and prices = List((Plain Donut,1.5), (Strawberry Donut,2.0), (Glazed Donut,2.5))

 

4. How to unzip the zipped donut sequence into separate donuts names and prices Sequences

In contrast to the zip method, calling unzip on the tupled collection from Step 3 will un-merge the String and Double collections into separate ones.


println("\nStep 4: How to unzip the zipped donut sequence into separate donuts names and prices Sequences")
val unzipped: (Seq[String], Seq[Double]) = zippedDonutsAndPrices.unzip
println(s"Donut names unzipped = ${unzipped._1}")
println(s"Donut prices unzipped = ${unzipped._2}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to unzip the zipped donut sequence into separate donuts names and prices Sequences
Donut names unzipped = List(Plain Donut, Strawberry Donut, Glazed Donut)
Donut prices unzipped = List(1.5, 2.0, 2.5)

 

This concludes our tutorial on Learn How To Use Unzip Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize a Sequence of donuts
  • How to initialize a Sequence of donut prices
  • How to zip the donuts Sequence with their corresponding prices
  • How to unzip the zipped donut sequence into separate donuts names and prices Sequences

Tip

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use the unzip3 function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: