Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert tuple to a List of first item

Tags:

scala

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.

like image 556
Drew H Avatar asked Dec 19 '11 14:12

Drew H


People also ask

How do you get the first item from a tuple?

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.

Can you convert a tuple to 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.

How do I get the first element of a list?

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.

How do I get the first element of a list in Python?

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.


1 Answers

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.

like image 145
Landei Avatar answered Oct 21 '22 05:10

Landei