# How to iterate over a nested dictionary separating the values

**URL:** <https://forum.robotframework.org/t/how-to-iterate-over-a-nested-dictionary-separating-the-values/7430>\
**Category:** Robot Framework\
**Created:** [14 June 2024 13:44 UTC](https://forum.robotframework.org/t/how-to-iterate-over-a-nested-dictionary-separating-the-values/7430 "2024-06-14T13:44:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oli129](https://avatars.discourse-cdn.com/v4/letter/o/bb73d2/32.png) [@oli129](https://forum.robotframework.org/u/oli129)\
**Post date:** [14 June 2024 13:44 UTC](https://forum.robotframework.org/t/how-to-iterate-over-a-nested-dictionary-separating-the-values/7430/1 "2024-06-14T13:44:11Z")

</div>

Dear Team!

I am trying to automate a UI menu navigation verification and I do have a nested dictionary in a .py

tabs= {

```
'key_1': {
    'value_1',
    'value_2',
    'value_3'
},
'key_2': {
    'value_1',
    'value_2',
    'value_3',
    'value_4'
},
'key_3': {
    'value_1',
    'value_2'
}

```

I did create a keyword to load the keys into variables and similarly for values.

Keyword To Check Tab Visibility

```
FOR ${key} ${value} IN &{tabs}
    ${main_key} Set Variable ${key}
    Log ${main_key}

           FOR ${value} IN ${tabs}[${main_key}]
                      ${subkey} Set Variable ${value}
                      Log ${subkey} 
          END
END

```

What I am trying to achieve here is to verify the main tab and subtab visibility in a menu navigation bar by looking up the locator by the text coming from the dictionary. The key would be the main menu element and the values are its subtabs.

However, as it is a multi value dictionary, when I iterate over the values based on their parent keys, it will load all the values to the same variable.  
Like this:

${subkey}= {‘value\_1’, ‘value\_2’}

I would need to work with these values separately. Is there a way to split these values?

I am likely to over complicate this, if you could please bear with me, I would higly appreciate.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [15 June 2024 01:59 UTC](https://forum.robotframework.org/t/how-to-iterate-over-a-nested-dictionary-separating-the-values/7430/2 "2024-06-15T01:59:27Z")

</div>

Hi Oliver,

Lets start with your data structure:

```python
'key_1': {
    'value_1',
    'value_2',
    'value_3'
},

```

I’m not sure how python will handle this, even though its declared as a dictionary with the curly braces (`{` and `}`) it’s not a dictionary, because you’ve only given values? not key and value pairs. but likewise you didn’t use square braces (`[` and `]`) so it’s not a list either.

From your first line I believe you intended the value of ‘key\_1’ to be a dictionary, so this is the first thing to fix.

Now let’s move onto your robot script:  
This line is good:

```python
FOR ${key} ${value} IN &{tabs}

```

This line is unnecessary:

```python
    ${main_key} Set Variable ${key}

```

This line is problematic:

```python
           FOR ${value} IN ${tabs}[${main_key}]

```

- you already had declared `${value}` and now you’re overwriting it, robot framework will happily do this but it becomes confusing
- the structure of the `FOR` indicates your wanting to iterate a list, but the data should be a dictionary (see above), but you’ve use the scalar variable prefix (`$`) so `${tabs}[${main_key}]` will probably evaluate to something like this `['value_1, value_2, value_3']`, and `${subkey}` will become `'value_1, value_2, value_3'`
- instead of `${tabs}[${main_key}]` you could have used `${value}`, but used the correct prefix, i.e. `&{value}` for dictionary or `@{value}` for list

So now you know what the problems are, here’s my attempt to fix it for you based on what I think you wanted

your data (tabs)

```python
tabs= {
    'key_1': {
        'key_1_1': 'value_1',
        'key_1_2': 'value_2',
        'key_1_3': 'value_3'
    },
    'key_2': {
        'key_2_1': 'value_1',
        'key_2_2': 'value_2',
        'key_2_3': 'value_3',
        'key_2_4': 'value_4'
    },
    'key_3': {
        'key_3_1': 'value_1',
        'key_3_2': 'value_2'
    }
}

```

and your robot for loop:

```python
FOR ${main_key} ${main_value} IN &{tabs}
    Log ${main_key}
    FOR ${sub_key} ${sub_value} IN &{main_value}
        Log ${sub_key} 
        Log ${sub_value} 
    END
END

```

Hopefully this helps,

Dave.

---

<div class="post-metadata">

**Author:** ![oli129](https://avatars.discourse-cdn.com/v4/letter/o/bb73d2/32.png) [@oli129](https://forum.robotframework.org/u/oli129)\
**Post date:** [16 June 2024 00:35 UTC](https://forum.robotframework.org/t/how-to-iterate-over-a-nested-dictionary-separating-the-values/7430/3 "2024-06-16T00:35:25Z")

</div>

Dear Dave!

Thank you very much for taking the time to explain and correct my mistakes and my poor coding! I do really appreciate that!

I would have probably been better off with a simple list, I still need to think about that. I tend to over complicate my own codes.

You are right, I did give an improper dictionary example in my original post! That was going to be a key value pair everywhere!

All I am trying to accomplish here, is to validate the functionality of a menu bar, that has main menu buttons and once clicked they list a bunch of submenus, with the simplest way possibles. And still utilizing some decent coding and getting all the required locator data from this dictionary.

I would like to achieve this with a single keyword. And not having to declare all the locators one by one, instead using their tag texts to locate and click on them. So, their menu names would get extracted from this dictionary and the code would construct a proper locator that gets passed to a variable, that I could further work with.

This is the reason why I had thought of a FOR loop and a dictionary to set a key as the main menu and attach all the submenus as values to it.

You already helped a lot and I cannot be grateful enough!

Regards,  
Oliver

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [16 June 2024 00:55 UTC](https://forum.robotframework.org/t/how-to-iterate-over-a-nested-dictionary-separating-the-values/7430/4 "2024-06-16T00:55:46Z")

</div>

Hi Oliver,

Your welcome, it was reading explanations like this that helped me learn and understand many decades ago when I was learning, so I’m happy to pay it forward and help others, hopefully one day you’ll be able to as well.

If you can extract the menu structure as a dictionary structure like that (i.e. a dictionary of dictionaries) then the simple solution might be to use [Dictionaries Should Be Equal](https://robotframework.org/robotframework/latest/libraries/Collections.html#Dictionaries%20Should%20Be%20Equal) from `Collections` Library to compare the menu structure to the expected structure, then no need to iterate the dictionary at all.

Dave.
