Initialize List of Lists

I am trying to initialize a generic List of List of Strings - ex. ListColors = {{“red”, “rojo”}, {“blue”, “azul”}, {“green”, “verde”}}
This is just an example, in reality I don’t know the length of the sublists nor the length of the mainlist. All I know is that the sublists are only of type String.

When I usually initialize a generic List of String, I use the assign tool as such - ex. ListRed = New List(Of String)

So when considering another dimension, how do I initialize a generic List of List of Strings? This is the variable type I assumed of the mainlist, ListColors → System.Collections.Generic.List<System.Collections.Generic.List<System.String>>

If this cannot be done, I see another thread on the forum stating we can initialize an array of lists (of String). When running through a loop, how do I append list i to to the array. Do I use the Add collection tool?

Thanks for any help!

Hello @vijithhhhh

Welcome to the UiPath Community.

Try this-

listRed = new List(of Object)

and use add to collection after that to add list values.

Hi try this:

New List(Of List(Of String)) From {{“red”, “rojo”}.ToList(), {“blue”, “azul”}.ToList(), {“green”, “verde”}.ToList()}

1 Like

This worked, thank you!
mainlist is now a List of Objects, and each sublist is a generic list of String.
However, later on in the workflow, I need to retrieve the index of a given string in the given sublist. How do I find the index of a string in an Object?
Using the example from original post,
ListColors = {{“red”, “rojo”}, {“blue”, “azul”}, {“green”, “verde”}}
current_sublist = ListColors(1) --meaning I want to access blue sublist. current_sublist is currently initialized as an object, because if I initialize as generic list it gives an error.
color_index = current_sublist.IndexOf(“azul”) --meaning I want to return the position of “azul” in current_sublist. But this is returning an error.

1 Like

Never mind, I figured it out. I needed to initialize current_sublist as a generic list rather than an object, and use current_sublist = DirectCast(ListColors(1), List(Of String)) to convert the sublist object to a generic list. But thank you again for your earlier help!

1 Like

While you can definitely use a list of objects, I think there’s no need to complicate stuff by adding the need to cast the elements. Just as @asgRPA said, you can simply declare it as New List(Of List(Of String)) and then use add to collection to add List(of String) elements to it.

1 Like

Certainly.

It sounds like you’d be better off using a dictionary instead of list of lists (2 dimensional array).