Access items of a list, which is in a dictionary, with for loop

6,674 views
Skip to first unread message

crater...@gmail.com

unread,
Jan 26, 2017, 6:43:38 AM1/26/17
to robotframework-users
Greetings all,

I have a problem with for loop, list and dictionary. I have a dictionary, and one value in the dictionary is a list. So something like my_dictionary = {list_of items: [item1, item2, item3]}
I want to use for loop to loop over those items in the list, so I did:

:FOR    ${item}    IN    &{my_dictionary}[list_of_items]

However, in each iteration of for loop, ${item} is actually a list that contains a single item. I would like ${item} to not be a list, but simply item (e.g. item1, item2,...).
Thank you for hints
Best, JC




Pekka Klärck

unread,
Jan 27, 2017, 4:23:02 PM1/27/17
to crater...@gmail.com, robotframework-users
Hi,

You always need to use @{list} variables if you want the value to be
expanded. The problem is that you cannot really combine it with the
&{dict}[key] syntax in a single variable access. These ought to work
(but I didn't test them):

1. First get the items into a separate variable and then iterate over them:

@{items} = Set variable &{my_dictionary}[list_of_items]
:FOR ${item} IN @{items}
# ...

2. Access dictionary item using the extended variable syntax:

:FOR ${item} IN @{my_dictionary['list_ot_items']}

3. If the dictionary is created in Robot data, values ought to be
accessible using `dict.key` notation in addition to `dict['key']`.
That would simplify the above a little:

:FOR ${item} IN @{my_dictionary.list_ot_items}


Cheers,
.peke

PS: I've been thinking for some time that I should write a post
explaining the differences about ${scalar}, @{list} and &{dict}
variables. The documentation in the User Guide is somewhat confusing,
because the details about these different variables have changed over
the years and documentation hasn't been fully re-written.
> --
> You received this message because you are subscribed to the Google Groups
> "robotframework-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to robotframework-u...@googlegroups.com.
> To post to this group, send email to robotframe...@googlegroups.com.
> Visit this group at https://groups.google.com/group/robotframework-users.
> For more options, visit https://groups.google.com/d/optout.



--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org
Reply all
Reply to author
Forward
0 new messages