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
  • 445
  • 2
  • 5
  • 15

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
  • 12,728
  • 1
  • 22
  • 48
1

Consider also

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

though not so concise as a for comprehension.

elm
  • 19,337
  • 14
  • 61
  • 108