How To Use Dictionary instead of List if using FOR loop

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

${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

Hi Leo,

As you already have Append To List I’ll assume you already have Collections Library, so you just need to use Create Dictionary and Set To Dictionary. Something like this I guess:

${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.

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

${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

{'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

*** 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

{'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'}

Hi Leo,

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 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.

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

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

Ref:

1 Like