list1 = [ 1, 2, 3]
list2 = [4, 5, 6]
I want to perform addition on these lists in this way: 1+4, 2+5, 3+6.. and so on.
Any help would be really appreciated.
Thanks.
list1 = [ 1, 2, 3]
list2 = [4, 5, 6]
I want to perform addition on these lists in this way: 1+4, 2+5, 3+6.. and so on.
Any help would be really appreciated.
Thanks.
In Robot Framework there aren't many calculation keyword options in the BuiltIn Library. There are certainly no native ones and we often resort to using Evaluate to evaluate a python expression. This python expression is further explained in the already mentioned Element-wise addition of 2 lists. Combining both the the below code:
*** Test Cases ***
Map Lamda Calc
${list1} Create List ${1} ${2} ${3}
${list2} Create List ${1} ${2} ${3}
${CalcList} Evaluate map(lambda x, y: x + y, $list1, $list2)
Log To Console \n${CalcList}
Which will then result in the following console response:
==============================================================================
Test
==============================================================================
Test.calcLists
==============================================================================
Map Lamda Calc
[2, 4, 6]
| PASS |
------------------------------------------------------------------------------
Test.calcLists | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
============================================================================
In the code the ${1} construct is used to ensure that the value stored is indeed an robot framework integer. Otherwise a string is provided and then a concatenation is performed.
A robotframework native solution, using the for in-zip construct - it itterates over two lists, up to the length of the smaller one:
${list a}= Create List 1 5 3 8
${list b}= Create List 2 4 7
${result}= Create List
:FOR ${a} ${b} IN ZIP ${list a} ${list b}
\ ${sum}= Evaluate int($a) + int($b)
\ Append To List ${result} ${sum}
When ran, the value of ${result}
will be [3, 9, 10]
- as ${list b}
is with just 3 members.
Inside the evaluate all source lists members are casted to int - so if any is not a number-like object, the case/keyword will fail on that step.