| Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 1 | *** Settings *** | 
|  | 2 | Documentation  This module contains keywords for list manipulation. | 
|  | 3 | Library  Collections | 
|  | 4 |  | 
|  | 5 | *** Keywords *** | 
| Michael Walsh | a4e79ff | 2017-10-30 14:40:14 -0500 | [diff] [blame] | 6 | Smart Combine Lists | 
|  | 7 | [Documentation]  Combine all valid list arguments and return the result. | 
|  | 8 | [Arguments]  @{lists} | 
|  | 9 |  | 
|  | 10 | # Description of argument(s): | 
|  | 11 | # lists  A list of lists to be combined.  Any item in this list which is | 
|  | 12 | #        NOT a list will be removed. | 
|  | 13 |  | 
|  | 14 | ${list_size}=  Get Length  ${lists} | 
|  | 15 | ${index}=  Set Variable  ${0} | 
| Sushil Singh | 1988855 | 2020-06-08 02:14:52 -0500 | [diff] [blame] | 16 |  | 
|  | 17 | FOR  ${arg}  IN  @{lists} | 
|  | 18 | ${type_arg}=  Evaluate  str(type($lists[${index}])).split("'")[1] | 
|  | 19 | Run Keyword If  '${type_arg}' != 'list'  Run Keywords  Remove From List  ${lists}  ${index}  AND | 
|  | 20 | ...  Continue For Loop | 
|  | 21 | ${index}=  Evaluate  ${index}+1 | 
|  | 22 | END | 
| Michael Walsh | a4e79ff | 2017-10-30 14:40:14 -0500 | [diff] [blame] | 23 |  | 
|  | 24 | ${new_list}=  Combine Lists  @{lists} | 
|  | 25 |  | 
| George Keishing | 409df05 | 2024-01-17 22:36:14 +0530 | [diff] [blame] | 26 | RETURN  ${new_list} | 
| Michael Walsh | a4e79ff | 2017-10-30 14:40:14 -0500 | [diff] [blame] | 27 |  | 
|  | 28 |  | 
| Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 29 | Intersect Lists | 
|  | 30 | [Documentation]  Intersects the two lists passed in. Returns a list of | 
|  | 31 | ...  values common to both lists with no duplicates. | 
|  | 32 | [Arguments]  ${list1}  ${list2} | 
|  | 33 |  | 
|  | 34 | # list1      The first list to intersect. | 
|  | 35 | # list2      The second list to intersect. | 
|  | 36 |  | 
|  | 37 | ${length1}=  Get Length  ${list1} | 
|  | 38 | ${length2}=  Get Length  ${list2} | 
|  | 39 |  | 
|  | 40 | @{intersected_list}  Create List | 
|  | 41 |  | 
|  | 42 | @{larger_list}=  Set Variable If  ${length1} >= ${length2}  ${list1} | 
|  | 43 | ...                               ${length1} < ${length2}  ${list2} | 
|  | 44 | @{smaller_list}=  Set Variable If  ${length1} >= ${length2}  ${list2} | 
|  | 45 | ...                                ${length1} < ${length2}  ${list1} | 
|  | 46 |  | 
| Sushil Singh | 1988855 | 2020-06-08 02:14:52 -0500 | [diff] [blame] | 47 | FOR  ${element}  IN  @{larger_list} | 
|  | 48 | ${rc}=  Run Keyword and Return Status  List Should Contain Value  ${smaller_list}  ${element} | 
|  | 49 | Run Keyword If  '${rc}' == 'True'  Append to List  ${intersected_list}  ${element} | 
|  | 50 | END | 
| Leah McNutt | c9c9cde | 2016-10-07 16:53:52 +0000 | [diff] [blame] | 51 |  | 
|  | 52 | @{intersected_list}=  Remove Duplicates  ${intersected_list} | 
|  | 53 |  | 
| George Keishing | 409df05 | 2024-01-17 22:36:14 +0530 | [diff] [blame] | 54 | RETURN  @{intersected_list} | 
| Rahul Maheshwari | a93b8c0 | 2017-05-12 05:23:00 -0500 | [diff] [blame] | 55 |  | 
|  | 56 |  | 
|  | 57 | Subtract Lists | 
|  | 58 | [Documentation]  Subtract list 2 from list 1 and return the result. | 
|  | 59 | #  Return list contain items from the list 1 which are not present | 
|  | 60 | #  in the list 2. | 
|  | 61 | [Arguments]  ${list1}  ${list2} | 
|  | 62 | # Description of argument(s): | 
|  | 63 | # list1      The base list which is to be subtracted from. | 
|  | 64 | # list2      The list which is to be subtracted from list1. | 
|  | 65 |  | 
|  | 66 | ${diff_list}=  Create List | 
| manashsarma | 146d4db | 2020-06-05 07:43:07 -0500 | [diff] [blame] | 67 | FOR  ${item}  IN  @{list1} | 
|  | 68 | ${status}=  Run Keyword And Return Status | 
|  | 69 | ...  Should Contain  ${list2}  ${item} | 
|  | 70 | Run Keyword If  '${status}' == '${False}' | 
|  | 71 | ...  Append To List  ${diff_list}  ${item} | 
|  | 72 | END | 
| Rahul Maheshwari | a93b8c0 | 2017-05-12 05:23:00 -0500 | [diff] [blame] | 73 |  | 
| George Keishing | 409df05 | 2024-01-17 22:36:14 +0530 | [diff] [blame] | 74 | RETURN  ${diff_list} |