# How To Use Dictionary instead of List if using FOR loop

**URL:** <https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660>\
**Category:** Robot Framework\
**Created:** [7 May 2023 07:43 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660 "2023-05-07T07:43:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk111](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mk111](https://forum.robotframework.org/u/mk111)\
**Post date:** [7 May 2023 07:43 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660/1 "2023-05-07T07:43:17Z")

</div>

I want to create an dictionary for that I have following code:

```python
${text_List}= Create List #1st list
FOR ${list} IN RANGE 1 ${element_count}
  ${list_text}= Get Text ${list}
  Append To List ${text_List} ${list_text}
END

FOR ${element} IN RANGE 1 ${total_list_count}+1 #list count is same as ${text_List} 1st list
  @{Text_List} Create List
  ${web_elements}= Get WebElements ${xpath}
  FOR ${ele} IN ${web_elements}
      ${element_txt}= Get Text ${ele}
       Append To List ${Text_List} ${element_txt}
   END
END 

```

Here How Can I create Dictionary instead of List in ROBOT framework where ${text\_List} should be Key and ${Text\_List} should be value like  
key= ${text\_List}[${1st index}  
value= ${Text\_List}[${1st index} same for other index position

---

<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:** [7 May 2023 08:18 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660/2 "2023-05-07T08:18:08Z")

</div>

Hi Leo,

As you already have `Append To List` I’ll assume you already have [Collections Library](https://robotframework.org/robotframework/latest/libraries/Collections.html), so you just need to use [Create Dictionary](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Create%20Dictionary) and [Set To Dictionary](https://robotframework.org/robotframework/latest/libraries/Collections.html#Set%20To%20Dictionary). Something like this I guess:

```python
${text_List}= Create List #1st list
FOR ${list} IN RANGE 1 ${element_count}
  ${list_text}= Get Text ${list}
  Append To List ${text_List} ${list_text}
END

FOR ${element} IN RANGE 1 ${total_list_count}+1 #list count is same as ${text_List} 1st list
  ${list_text}= Get Text ${list}
  &{Text_Dict} Create Dictionary
  ${web_elements}= Get WebElements ${xpath}
  FOR ${ele} IN ${web_elements}
      ${element_txt}= Get Text ${ele}
       # Append To List ${Text_List} ${element_txt}
       Set To Dictionary ${Text_Dict} ${list_text} ${element_txt}
   END
END

```

Dave.

---

<div class="post-metadata">

**Author:** ![mk111](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mk111](https://forum.robotframework.org/u/mk111)\
**Post date:** [7 May 2023 09:24 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660/3 "2023-05-07T09:24:07Z")

</div>

@damies13 Thanks for the response  
Actually I forgot to add that

```python
${text_List}= Create List #1st list
FOR ${list} IN RANGE 3 ${element_count}+1 #count depends on element present but it will be same as ${total_list_count} below
  ${list_text}= Get Text ${xpath}${list}
  Append To List ${text_List} ${list_text}
END
#expected output= ['124', '1445', '1123', '456', '2354'] 

FOR ${element} IN RANGE 1 ${total_list_count}+1 #list count is same as ${text_List} 1st list count
  @{Text_List} Create List
  ${web_elements}= Get WebElements ${xpath}
  FOR ${ele} IN ${web_elements}
      ${element_txt}= Get Text ${ele}
       Append To List ${Text_List} ${element_txt}
   END
END

```

Here in the 2nd FOR loop there will be n number of matching xpath so it will get all the text values one by one then it will go to next column and so on  
So i want final output values as

```python
{'124': 'for loop text one by one for that column', '1445': 'for loop text one by one for next column', '1223': 'for loop text one by one for that next column', '456': 'for loop text one by one for that next column', '2354': 'for loop text one by one for that next column'}

```

Sorry for the wrong question asked earlier is this possible in robot framework  
After getting all the text values column wise I want to do addition of values in dictionary

---

<div class="post-metadata">

**Author:** ![mk111](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mk111](https://forum.robotframework.org/u/mk111)\
**Post date:** [7 May 2023 09:39 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660/4 "2023-05-07T09:39:08Z")

</div>

\*\*\* Test Cases \*\*\*  
Example Test Case  
${text\_dict}= Create Dictionary # Create an empty dictionary named ${text\_dict}  
FOR ${list} IN RANGE 3 ${element\_count}+1  
${list\_text}= Get Text ${xpath}${list} # Get the text value of the ${list} element  
Set To Dictionary ${text\_dict} ${list\_text} ${EMPTY} # Add the text value as key to the dictionary with an empty value  
END

```
FOR ${element} IN RANGE 1 ${total_list_count}+1 # Loop through a range of values which is same as the count of elements in ${text_dict}
    ${web_elements}= Get WebElements ${xpath} # Get a list of web elements using an xpath expression
    FOR ${ele} IN ${web_elements} # Loop through the web elements
        ${element_txt}= Get Text ${ele} # Get the text value of the web element
        ${key}= Get From Dictionary ${text_dict} ${element} # Get the key value from ${text_dict} using ${element} as index
        Set To Dictionary ${text_dict} ${key} ${element_txt} # Set the value of the key in ${text_dict} with ${element_txt}
    END
END 

Log Dictionary ${text_dict} # Log the dictionary after all the key-value pairs have been added to it

```

Tried with above code but got error as Dictionary does not contain key ‘1’  
to get output as

```python
{'124': 'for loop text one by one for that column', '1445': 'for loop text one by one for next column', '1223': 'for loop text one by one for that next column', '456': 'for loop text one by one for that next column', '2354': 'for loop text one by one for that next column'}

```

---

<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:** [7 May 2023 10:10 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660/5 "2023-05-07T10:10:42Z")

</div>

Hi Leo,

> [@mk111](#):
>
> Dictionary does not contain key ‘1’

My example was just a guess, I didn’t actually expect it to work, more importantly I was letting you know about the keywords for creating/populating a dictionary.

Without knowing what your data looks like I can only guess, but with dictionaries you don’t select values by an index number as you do in lists, rather you select them by their key.

I’ll suggest as a diagnostic step you use the [Log](https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Log) keyword to log the dictionary before trying to retrieve a value from it, then if your test fails you can check the result of Log to see what was in the dictionary and that will give you a clue as to why you got the error.

Dave.

---

<div class="post-metadata">

**Author:** ![mk111](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mk111](https://forum.robotframework.org/u/mk111)\
**Post date:** [7 May 2023 10:55 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660/6 "2023-05-07T10:55:44Z")

</div>

> [@mk111](#):
>
> \*\*\* Test Cases \*\*\*  
> Example Test Case  
> ${text\_dict}= Create Dictionary # Create an empty dictionary named ${text\_dict}  
> FOR ${list} IN RANGE 3 ${element\_count}+1  
> ${list\_text}= Get Text ${xpath}${list} # Get the text value of the ${list} element  
> Set To Dictionary ${text\_dict} ${list\_text} ${EMPTY} # Add the text value as key to the dictionary with an empty value  
> END
> 
> ```python
> FOR ${element} IN RANGE 1 ${total_list_count}+1 # Loop through a range of values which is same as the count of elements in ${text_dict}
> ${web_elements}= Get WebElements ${xpath} # Get a list of web elements using an xpath expression
> FOR ${ele} IN ${web_elements} # Loop through the web elements
> ${element_txt}= Get Text ${ele} # Get the text value of the web element
> ${key}= Get From Dictionary ${text_dict} ${element} # Get the key value from ${text_dict} using ${element} as index
> Set To Dictionary ${text_dict} ${key} ${element_txt} # Set the value of the key in ${text_dict} with ${element_txt}
> END
> END 
> 
> Log Dictionary ${text_dict}
> 
> ```

Input will be the text present on boxes in column ( like 12345) there will be lots of boxes depending on option selected

---

<div class="post-metadata">

**Author:** ![\_daryl](https://avatars.discourse-cdn.com/v4/letter/_/45deac/32.png) [@\_daryl](https://forum.robotframework.org/u/_daryl)\
**Post date:** [8 May 2023 00:48 UTC](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-list-if-using-for-loop/5660/7 "2023-05-08T00:48:52Z")

</div>

Looks to be in part a duplicate (for which I replied and then seen the post here after)

Ref:

> [@How to use dictionary instead of of list in robot framework](https://forum.robotframework.org/t/how-to-use-dictionary-instead-of-of-list-in-robot-framework/5662/2):
>
> Basically as the error reads, Its saying it doesn’t contain key ‘1’ Which ${element} will in your case equal either 1 or a number from that upto your list count+1 variable you’ve provided (your examples above don’t show the value assigned to these or how the count is returned) the line from above: FOR ${element} IN RANGE 1 ${total\_list\_count}+1 as that’s the value your giving the kw to get from the dictionary: ${key}= Get From Dictionary ${text\_dict} ${element} for which if there…
