For Each Loop List and Dictionary - Visual Scripting / Bolt

Hello everyone,

I’m new in visual scripting and trying to add 2 AOT Lists to an AOT Dictionary variable using For Each Loop like this

When I run it the result is like this

I expect the result is
a: 1
b: 2
c: 3
d: 4
e: 5

Can somebody help me fix the graph so the result is just what I expected? thank you.

Hello there!

What you are doing is:

void Start()
{
    foreach(string str in strings)
        foreach(int integer in ints)
            if(!dictionary.ContainsKey(str))
                dictionary.Add(str, integer);
}

What I suspect you want is something like this (only one loop):

void Start()
{
    for(int i = 0; i < strings.Count; i++)
        if(!dictionary.ContainsKey(strings[i]))
            dictionary.Add(strings[i], ints[i]);
}

So you should use one for each loop and use the List.GetItem method with the index given by the for each loop to get the value of the second list.

9784443--1403622--upload_2024-4-21_17-40-38.png

Means I should use one loop and one for each loop? What I got based on your answer is something like this, cmiiw.

and yet I still don’t know where to connect for each loop to get the value of the second list.

You only need one loop.
The GetItem node get you the element of your second list if you provide the current index of the loop.
Something like this

9784782--1403721--upload_2024-4-21_21-33-41.png

I don’t see your screenshot.

1 Like

Sorry about the screenshot, I’m updating with your last correction and be like this

It works, thank you.

9785049--1403772--upload_2024-4-22_6-56-2.png
9785049--1403775--upload_2024-4-22_7-6-7.png

1 Like