Your lists are List[AnyRef]
. That's a type that should never appear in ordinary Scala code.
I would suggest representing your data using List[Option[String]]
:
scala> List(List(Some("XX"), None, None, None),
| List(None, Some("YY"), None, None),
| List(None, None, None, Some("ZZ")))
res2: List[List[Option[String]]] = List(...
Now your problem is easy to solve:
scala> res2.transpose.map(_.flatten.headOption)
res6: List[Option[String]] = List(Some(XX), Some(YY), None, Some(ZZ))
I've assumed here that if there are multiple Some
s in the same position, you want to take the first one.