blob: 63384d39ec27d06ccecc0115708875f87258ab8e [file] [log] [blame]
Leah McNuttc9c9cde2016-10-07 16:53:52 +00001*** Settings ***
2Documentation This module contains keywords for list manipulation.
3Library Collections
4
5*** Keywords ***
Michael Walsha4e79ff2017-10-30 14:40:14 -05006Smart 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 Singh19888552020-06-08 02:14:52 -050016
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 Walsha4e79ff2017-10-30 14:40:14 -050023
24 ${new_list}= Combine Lists @{lists}
25
26 [Return] ${new_list}
27
28
Leah McNuttc9c9cde2016-10-07 16:53:52 +000029Intersect 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 Singh19888552020-06-08 02:14:52 -050047 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 McNuttc9c9cde2016-10-07 16:53:52 +000051
52 @{intersected_list}= Remove Duplicates ${intersected_list}
53
Gunnar Millsc9ea9362016-12-13 16:21:13 -060054 [Return] @{intersected_list}
Rahul Maheshwaria93b8c02017-05-12 05:23:00 -050055
56
57Subtract 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
manashsarma146d4db2020-06-05 07:43:07 -050067 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 Maheshwaria93b8c02017-05-12 05:23:00 -050073
74 [Return] ${diff_list}