TestingK
TestingK

Reputation: 13

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}

Upvotes: 1

Views: 843

Answers (2)

Squrppi
Squrppi

Reputation: 95

Simple way to reduce a list from another list

*** Settings ***
Library     Collections


*** Test Cases ***
Difference in Lists
    ${list1} =    Create List    a    b    c    d    e    f    g
    ${list2} =    Create List         b    c              f    g
    Remove Values From List    ${list1}    @{list2}
    Log To Console    ${list1}

Will result in

==============================================================================
Difference in Lists                                                   ...['a','d', 'e']
Difference in Lists                                                   | PASS |
------------------------------------------------------------------------------

Upvotes: 0

Helio
Helio

Reputation: 3737

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}
      

Upvotes: 2

Related Questions