Iterate over dictionary

3,842 views
Skip to first unread message

Ladislav Folta

unread,
Oct 21, 2013, 10:44:04 AM10/21/13
to robotframe...@googlegroups.com
Hello,

is it possible to iterate over dictionary in robot like I can do in python:

dict = {'a' : 'oiu', 'b' : 'iuy'}

for key, value in dict.iteritems():
  print 'key: ' + key
  print 'value: ' + value

?

Curtis Matthews

unread,
Oct 22, 2013, 1:51:04 PM10/22/13
to robotframe...@googlegroups.com
There is a library called Collections that contains keywords that you can use to manipulate dictionaries. Don't know if this is the most Pythonic solution but it works if you import Collections into your test suite:
Iterate on Dictionary
    ${dict}=    Create Dictionary    a    oiu    b    iuy
    @{keys}=    Get Dictionary Keys    ${dict}    # keys will be a list of key items
    :FOR    ${key}    IN    @{keys}
    \    Log    Key: ${key}
    \    ${item}=    Get From Dictionary    ${dict}    ${key}
    \    Log    Item: ${item}

Kevin O.

unread,
Oct 22, 2013, 9:58:31 PM10/22/13
to robotframe...@googlegroups.com
An alternative, also using the Collections library that Curtis mentioned:
    ${dict}=    Create Dictionary    a    oiu    b    iuy
    ${items}=    Get Dictionary Items    ${dict}
    :FOR    ${key}    ${value}    IN    @{items}
    \    Log    ${key} : ${value}
Reply all
Reply to author
Forward
0 new messages