Using And Looping Over a List of Lists – C#

You might be familiar with how to loop over a list, but what about a list of lists? You may not even be sure how to create one. This post will cover very simply how to create, use, and loop over a list of lists.

If you are new to arrays, though, you might want to look at my overview on arrays first.

Create A List of Lists

To create the List of Lists, you do it like so:

        List<List<string>> itemBag= new List<List<string>>();

itemBag is our list of lists. In this case, we will be using lists of strings to create a character inventory from an RPG game.

Populating The List of Lists

This is how we add items to our list of lists. First create a temporary list, then add things to it, finally add that to our itemBag:

        List<string> weapons = new List<string>();
        weapons.Add("Sword");
        weapons.Add("Dagger");
        weapons.Add("Crossbow");

        itemBag.Add(weapons);

        List<string> potions = new List<string>();
        potions.Add("Health Potion");
        potions.Add("Strength Potion");
        potions.Add("Luck Potion");

        itemBag.Add(potions);

Looping Over A List of Lists

Now we can loop over our list of lists:

        for (int a = 0; a < itemBag.Count; a++)
        {
            for (int b = 0; b < itemBag[a].Count; b++)
            {
                Console.WriteLine(itemBag[a][b]);
            }
        }

If you have never looped over an array before, it’s all covered in my overview on arrays.

We need two loops, an outer loop to find each pocket where we store the different items in our itemBag, and then an inner loop to go over each item for each pocket.

Test and run. If you are using Unity, replace Console.WriteLine with Debug.Log.

You should see this:

Sword
Dagger
Crossbow
Health Potion
Strength Potion
Luck Potion

You will notice that our double loop found the weapons pocket first, and displayed all the weapons in that pocket, and then it found the potions pocket, and went over all of the potions in that pocket.

And that’s it – that’s how you loop over a list of lists. Full code below.

Full Code

        List<List<string>> itemBag = new List<List<string>>();

        List<string> weapons = new List<string>();
        weapons.Add("Sword");
        weapons.Add("Dagger");
        weapons.Add("Crossbow");

        itemBag.Add(weapons);

        List<string> potions = new List<string>();
        potions.Add("Health Potion");
        potions.Add("Strength Potion");
        potions.Add("Luck Potion");

        itemBag.Add(potions);

        for (int a = 0; a < itemBag.Count; a++)
        {
            for (int b = 0; b < itemBag[a].Count; b++)
            {
                Console.WriteLine(itemBag[a][b]);
            }
        }

        Console.ReadKey();

If you enjoyed this article, try my game Puzzledorf out on Steam and Humble Bundle.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: