Reputation: 151
I have list of tuple pairs, List[(String,String)]
and want to flatten it to a list of strings, List[String]
.
Upvotes: 12
Views: 12662
Reputation: 1043
See -
https://stackoverflow.com/a/43716004/4610065
In this case -
import syntax.std.tuple._
List(("John","Paul"),("George","Ringo")).flatMap(_.toList)
Upvotes: 0
Reputation: 20415
In general for lists of tuples of any arity, consider this,
myTuplesList.map(_.productIterator.map(_.toString)).flatten
Note the productIterator
casts all types in a tuple to Any
, hence we recast values here to String
.
Upvotes: 3
Reputation: 1725
Try:
val tt = List(("John","Paul"),("George","Ringo"))
tt.flatMap{ case (a,b) => List(a,b) }
This results in:
List(John, Paul, George, Ringo)
Upvotes: 6
Reputation: 6242
Well, you can always use flatMap as in:
list flatMap (x => List(x._1, x._2))
Although your question is a little vague.
Upvotes: 10
Reputation: 9100
Some of the options might be: concatenate:
list.map(t => t._1 + t._2)
one after the other interleaved (after your comment it seems you were asking for this):
list.flatMap(t => List(t._1, t._2))
split and append them:
list.map(_._1) ++ list.map(_._2)
Upvotes: 21