RanH
RanH

Reputation: 852

How to create a list from specific indexes in list of lists in Kotlin?

I would like to retrieve a list, containing all elements at a specific index from list of lists in Kotlin, for example:

List1 = [a,b,c,e]
List2 = [1,2,3,4]
List3 = [!,@,#,$]

Input = [List1,List2,List3]
Index = 2

Desired output: [c,3,#]

How do I do it efficiently in Kotlin? Is there a better way than looping through the lists? Thanks

Upvotes: 0

Views: 949

Answers (1)

broot
broot

Reputation: 28332

Input.map { it[Index] }

It iterates through all sublists and takes Index element from each of them.

Note that by creating a list of lists you lose information about the type of each sublist. You would need to redesign your data structure to keep this information.

Upvotes: 1

Related Questions