0

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.

Abhishek A.P
  • 5
  • 1
  • 7
  • http://stackoverflow.com/questions/18713321/element-wise-addition-of-2-lists possible duplicate of this. – Waman Mar 30 '17 at 04:40
  • 1
    Possible duplicate of [Element-wise addition of 2 lists?](http://stackoverflow.com/questions/18713321/element-wise-addition-of-2-lists) – Max Mar 30 '17 at 04:57
  • The question is about doing that type of calculation in Robot Framework. To me native RF script does not equal Python so these two duplicate references are incorrect. – A. Kootstra Mar 30 '17 at 05:32

2 Answers2

2

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.

Community
  • 1
  • 1
A. Kootstra
  • 6,827
  • 3
  • 20
  • 43
0

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.

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60