1
List(1,2,3,4,5) partition (_ % 2 == 0)

produces

res40: (List[Int], List[Int]) = (List(2, 4),List(1, 3, 5))

How do I access the lists individually. res40(0) does not seem to work.

zam
  • 451
  • 2
  • 5
  • 21

2 Answers2

2

You can do this to assign each partition to a different val

val (even, odd) = List(1,2,3,4,5) partition (_ % 2 == 0)
Mikel San Vicente
  • 3,831
  • 2
  • 21
  • 39
1

partition creates a pair/tuple, and you can use ._1, ._2, etc to access elements in scala tuples, see related question:

res0._1
# res2: List[Int] = List(2, 4)
Community
  • 1
  • 1
Psidom
  • 209,562
  • 33
  • 339
  • 356