Scala nested Lists

Hello,

I’m trying to fetch value in nested lists in Scala but somehow I’m not able to find a solution.

I’ve created a list as:

var list3 = List((1,2,3),(4,5,6),(7,8),(9,10,11,12))

I want 7 as the output.

In python, for the same thing, I’m creating list as
list3 = [[1,2,3],[4,5,6],[7,8],[9,10,11,12]]

To get the output as 7, I’m using
list3[len(list3) -2][len(list3) -4]

Please suggest me how to achieve the same in Scala.

Thanks in advance.

The one you have defined is a list of tuples not list of lists.

var list3 = List(List(1,2,3),List(4,5,6),List(7,8),List(9,10,11,12))

And then you can use:

list3(list3.size - 2)(list3.size-4)

Is this what you are looking for?

Hi Sandeep,

Yes, I got this point and mistakenly I was trying with ’ list of tuples’ and not ‘list of lists’. It’s all clear now.
:slight_smile:

Thanks