0

I have two lists, namely

val a = List(1,2,3)

val b = List(4,5)

I want to perform N to N bipartite mapping and want to get output

List((1,4),(1,5),(2,4),(2,5),(3,4),(3,5))

How can I do this?

Mahek Shah
  • 485
  • 2
  • 5
  • 17

2 Answers2

5

Assuming that B = List(4,5), then you can use for comprehensions to achieve your goal:

val A = List(1,2,3)
val B = List(4,5)

val result = for(a <- A; b <- B) yield {
  (a, b)
}

The output is

result:List[(Int, Int)] = List((1,4), (1,5), (2,4), (2,5), (3,4), (3,5))
Till Rohrmann
  • 13,148
  • 1
  • 25
  • 51
1

Consider also

a.flatMap(x => b.map(y => (x,y)))

though not so concise as a for comprehension.

elm
  • 20,117
  • 14
  • 67
  • 113