Say I have a method that returns this.
Vector[ (PkgLine, Tree) ]()
I want to convert this to a List of PkgLines. I want to drop the Tree off. I'm not seeing anything in the scala library that would allow me to do this. Anybody have any simple ideas? Thanks.
We can iterate through the entire list of tuples and get first by using the index, index-0 will give the first element in each tuple in a list.
Python list method list() takes sequence types and converts them to lists. This is used to convert a given tuple into list. Note − Tuple are very similar to lists with only difference that element values of a tuple can not be changed and tuple elements are put between parentheses instead of square bracket.
Approach #2 : Using zip and unpacking(*) operator This method uses zip with * or unpacking operator which passes all the items inside the 'lst' as arguments to zip function. Thus, all the first element will become the first tuple of the zipped list. Returning the 0th element will thus, solve the purpose.
To access the first element (12) of a list, we can use the subscript syntax [ ] by passing an index 0 . Note: In Python lists are zero-indexed, so the first element is available at index 0 , second element index is 1, etc. Similarly, we can also use the slicing syntax [:1] to get the first element of a list in Python.
val list = vector.map(_._1).toList
If you have a Tupel t
, you can access its first element using t._1
. So with the map
operation, you're effectively throwing away the trees, and store the PkgLines
directly. Then you simply convert the Vector
to List
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With