Reputation: 5459
I have a nested list that I am using in Robot Framework. I would like to change one item in a sublist at the Robot Framework level.
My list looks like this:
[ bob, mary, [june, july, august]]
I want to change "july" to something else, say "september"
Robot Framework will let me change 'bob' or 'mary', but if I try to insert a list, it is converted into strings.
(Oh, I have tried to use the "Insert Into List keyword to insert a new sub list, and other List keywords, no luck with any.)
Upvotes: 5
Views: 18706
Reputation: 20067
The normal method "Set List Value" in the Collections library does work on the embedded list - and its changed inplace, w/o recreating the objects; here's the POC:
${listy}= Create List a b
${inner}= Create List 1 2
Append To List ${listy} ${inner}
Log To Console ${listy} # prints "[u'a', u'b', [u'1', u'2']]", as expected
Set List Value ${listy[2]} 0 4
# ^ changes the 1st element of the embedded list to "4" - both the listy's index (2), and the kw argument (0) can be variables
Log To Console ${listy} # prints "[u'a', u'b', [u'4', u'2']]" - i.e. updated
Upvotes: -1
Reputation: 5459
I am guessing from the lack of response that there isn't a neat clean solution to this. Here is what I have done:
I created a utility thusly:
class Pybot_Utilities:
def sublistReplace(self, processList, item, SublistIndex, ItemIndex):
'''
Replaces an item in a sublist
Takes a list, an object, an index to the sublist, and an index to a location in the sublist inserts the object into a sublist of the list at the location specified.
So if the list STUFF is (X, Y, (A,B,C)) and you want to change B to FOO give these parameters: [STUFF, FOO, 2, 1]
'''
SublistIndex=int(SublistIndex)
ItemIndex=int(ItemIndex)
processList[SublistIndex][ItemIndex] = str(item)
return processList
I then put this entry in my robot framework test suite file:
| | ${ListWithSublist} = | sublistReplace | ${ListWithSublist]} | NewItem | 1 | 1 |
(Importing my utility library, of course)
After this runs, the second item (index 1) in the sublist at index 1 of the list will be "NewItem"
Perhaps not the most elegant or flexible, but it will do the job for now
Upvotes: 3
Reputation: 1017
I was able to achieve the modification with Collections library keywords like this
*** settings ***
Library Collections
*** test cases ***
test ${l1}= Create List 1 2 3
${l2}= Create List foo bar ${l1}
${sub}= Get From List ${l2} 2
Set List Value ${sub} 2 400
Set List Value ${l2} 2 ${sub}
Log ${l2}
I was not able to find a way to directly alter the sublist, it has to be first extracted, then modified and finally put back in place.
Upvotes: 5