0

I have the following input:

List(
 Map("A" -> 1, "B" -> 2, "C" -> 3),
 Map("A" -> 4, "B" -> 5, "C" -> 6),
 Map("A" -> 7, "B" -> 8, "C" -> 9)
)

which I want to transform into:

Map(
 "A" -> List(1,4,7),
 "B" -> List(2,5,8),
 "C" -> List(3,6,9)
)

I have tried to use transpose but I'm not getting anywhere.

kms333
  • 3,147
  • 7
  • 31
  • 39
  • 1
    Possible duplicate of [Scala: Merge maps by key](https://stackoverflow.com/questions/7755214/scala-merge-maps-by-key) (and several other questions) – The Archetypal Paul Sep 05 '17 at 10:00

1 Answers1

2

You need to flatten, then groupBy and then mapValues to keep the list

list.flatten.groupBy(_._1).mapValues(_.map(_._2))
Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39