Chirag
Chirag

Reputation: 57

How to merge two Seq[String], Seq[Double] to Seq[(String,Double)]

I have two Seq. 1 has Seq[String] and another has Seq[(String,Double)]

a -> ["a","b","c"] and b-> [1,2,3]

I want to create output as

[("a",1),("b",2),("c",3)]

I have a code a.zip(b) is actually creating a seq of those two elements instead of creating a map

Can anyone suggest how to do that in scala?

Upvotes: 0

Views: 706

Answers (1)

prayagupadhyay
prayagupadhyay

Reputation: 31232

you simply need .toMap so that you can transform List[Tuple[String, Int]] to Map[String, Int]

scala> val seq1 = List("a", "b", "c")
seq1: List[String] = List(a, b, c)

scala> val seq2 = List(1, 2, 3)
seq2: List[Int] = List(1, 2, 3)

scala> seq1.zip(seq2)
res0: List[(String, Int)] = List((a,1), (b,2), (c,3))

scala> seq1.zip(seq2).toMap
res1: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)

also see

How to convert a Seq[A] to a Map[Int, A] using a value of A as the key in the map?

Upvotes: 2

Related Questions