Reputation: 852
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
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