Search code examples
pythonrobotframework

How to remove the element present in one list from another list using robotframework


I am very new to Robot framework and trying to print the value which is not common:

$list1= ['test1','test2','test3','test4'] $list2= ['test1','test2','test3','test4','test5']

${difference}= $list2-$list1 log to console ${difference}


Solution

  • Here is a possible example for the difference keyword:

    *** Settings ***
    Library           Collections
    
    *** Test Cases ***
    Difference of lists
        @{list1}=    Create List    test1    test2    test3    test4
        @{list2}=    Create List    test1    test2    test3    test4    test5
        @{list3}=    Create List    test4    test3    test1    test2
        ${diff_list}=    List Difference    ${list1}    ${list2}
        Log Many    @{diff_list}
        Should Be Equal As Strings    ${diff_list}    ['test5']
        ${diff_list}=    List Difference    ${list1}    ${list3}
        Log Many    @{diff_list}
        Should Be Equal As Strings    ${diff_list}    []
    
    *** Keywords ***
    List Difference
        [Arguments]    ${arg1}    ${arg2}
        @{new_list}=    Create List    @{arg1}    @{arg2}
        FOR    ${item}    IN    @{arg1}
            Remove Values From List    ${new_list}    ${item}
        END
        RETURN    ${new_list}