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