blob: ebdef6f2089f61418ab0d8eeeaa0c0ff4824fc19 [file] [log] [blame]
Leah McNuttc9c9cde2016-10-07 16:53:52 +00001*** Settings ***
2Documentation This module contains keywords for list manipulation.
Sridevi Ramesh8bd280d2025-09-21 07:03:43 -05003
Leah McNuttc9c9cde2016-10-07 16:53:52 +00004Library Collections
5
6*** Keywords ***
Sridevi Ramesh8bd280d2025-09-21 07:03:43 -05007
Michael Walsha4e79ff2017-10-30 14:40:14 -05008Smart 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 Singh19888552020-06-08 02:14:52 -050018
19 FOR ${arg} IN @{lists}
20 ${type_arg}= Evaluate str(type($lists[${index}])).split("'")[1]
Sridevi Ramesh8bd280d2025-09-21 07:03:43 -050021 IF '${type_arg}' != 'list'
22 Remove From List ${lists} ${index}
23 Continue For Loop
24 END
Sushil Singh19888552020-06-08 02:14:52 -050025 ${index}= Evaluate ${index}+1
26 END
Michael Walsha4e79ff2017-10-30 14:40:14 -050027
28 ${new_list}= Combine Lists @{lists}
29
George Keishing409df052024-01-17 22:36:14 +053030 RETURN ${new_list}
Michael Walsha4e79ff2017-10-30 14:40:14 -050031
32
Leah McNuttc9c9cde2016-10-07 16:53:52 +000033Intersect 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 Ramesh8bd280d2025-09-21 07:03:43 -050038 # Description of argument(s):
Leah McNuttc9c9cde2016-10-07 16:53:52 +000039 # 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 Singh19888552020-06-08 02:14:52 -050052 FOR ${element} IN @{larger_list}
53 ${rc}= Run Keyword and Return Status List Should Contain Value ${smaller_list} ${element}
Sridevi Ramesh8bd280d2025-09-21 07:03:43 -050054 IF '${rc}' == 'True' Append to List ${intersected_list} ${element}
Sushil Singh19888552020-06-08 02:14:52 -050055 END
Leah McNuttc9c9cde2016-10-07 16:53:52 +000056
57 @{intersected_list}= Remove Duplicates ${intersected_list}
58
George Keishing409df052024-01-17 22:36:14 +053059 RETURN @{intersected_list}
Rahul Maheshwaria93b8c02017-05-12 05:23:00 -050060
61
62Subtract 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
manashsarma146d4db2020-06-05 07:43:07 -050072 FOR ${item} IN @{list1}
73 ${status}= Run Keyword And Return Status
74 ... Should Contain ${list2} ${item}
Sridevi Ramesh8bd280d2025-09-21 07:03:43 -050075 IF '${status}' == '${False}' Append To List ${diff_list} ${item}
manashsarma146d4db2020-06-05 07:43:07 -050076 END
Rahul Maheshwaria93b8c02017-05-12 05:23:00 -050077
George Keishing409df052024-01-17 22:36:14 +053078 RETURN ${diff_list}