Question

I have this type of input:
List( (key1, List(1,2,3)), (key2, List(4,5)) )
and I want to remap it the following way:
List( (key1, 1), (key1, 2), (key1, 3), (key2, 4), (key2, 5) )

I cannot figure out how to split a list into elements and still keep the key.

Was it helpful?

Solution 2

I'm not sure what you tried but here is the simplest way I would do it:

val splitMe = List( (key1, List(1,2,3)), (key2, List(4,5)) )
splitMe.flatMap(v=> v._2.map(g => (v._1, g)))

OTHER TIPS

val flattened = list.flatMap({ case (k, l) => l.map((k,_)) })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top