Given a Matrix, get all the rows with all the list elements.
Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [1, 2]
Output : [[2, 1, 8], [6, 1, 2]]
Explanation : Extracted lists have 1 and 2.
Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [2, 6]
Output : [[7, 6, 3, 2], [6, 1, 2]]
Explanation : Extracted lists have 2 and 6.
Method #1: Using loop
In this, we iterate for each row from Matrix, and check for the presence of each list element, if the present row is returned as a result. If any element is not present, row is flagged off.
Python3
test_list = [[ 7 , 6 , 3 , 2 ], [ 5 , 6 ], [ 2 , 1 , 8 ], [ 6 , 1 , 2 ]]
print ( "The original list is : " + str (test_list))
sub_list = [ 1 , 2 ]
res = []
for row in test_list:
flag = True
for ele in sub_list:
if ele not in row:
flag = False
if flag:
res.append(row)
print ( "Rows with list elements : " + str (res))
|
Output:
The original list is : [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]] Rows with list elements : [[2, 1, 8], [6, 1, 2]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #2 : Using all() + list comprehension
In this, all elements for presence as tested using all(), list comprehension is used as a one-liner to perform the task of iterating through rows.
Python3
test_list = [[ 7 , 6 , 3 , 2 ], [ 5 , 6 ], [ 2 , 1 , 8 ], [ 6 , 1 , 2 ]]
print ( "The original list is : " + str (test_list))
sub_list = [ 1 , 2 ]
res = [row for row in test_list if all (ele in row for ele in sub_list)]
print ( "Rows with list elements : " + str (res))
|
Output:
The original list is : [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]] Rows with list elements : [[2, 1, 8], [6, 1, 2]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #3: Using nested loops
Use a nested loops to iterate through the rows in test_list and the elements in sub_list, checking if each element in sub_list is present in each row of test_list. If all elements in sub_list are present in a row, the row is added to the result list res.
Python3
test_list = [[ 7 , 6 , 3 , 2 ], [ 5 , 6 ], [ 2 , 1 , 8 ], [ 6 , 1 , 2 ]]
print ( "The original list is : " + str (test_list))
sub_list = [ 1 , 2 ]
res = []
for row in test_list:
is_present = True
for ele in sub_list:
if ele not in row:
is_present = False
break
if is_present:
res.append(row)
print ( "Rows with list elements : " + str (res))
|
Output
The original list is : [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
Rows with list elements : [[2, 1, 8], [6, 1, 2]]
Time complexity: O(n^2), where n is the number of rows in test_list.
Auxiliary space: O(n), where n is the number of rows in test_list.
Method #4: Using set intersection
Approach:
- Convert the sub_list to a set for faster intersection operation.
- Initialize an empty list, res, to store the rows that contain all the elements of sub_list.
- Iterate through each row of the test_list.
- Convert the current row to a set.
- Take the intersection of the current row set and the sub_list set using the & operator.
- If the length of the intersection is equal to the length of sub_list, then all the elements of sub_list are present in the current row.
- Append the current row to the res list if all the elements of sub_list are present in the current row.
- Return the res list as the result.
Python3
test_list = [[ 7 , 6 , 3 , 2 ], [ 5 , 6 ], [ 2 , 1 , 8 ], [ 6 , 1 , 2 ]]
print ( "The original list is : " + str (test_list))
sub_list = [ 1 , 2 ]
sub_set = set (sub_list)
res = []
for row in test_list:
row_set = set (row)
intersection = row_set & sub_set
if len (intersection) = = len (sub_set):
res.append(row)
print ( "Rows with list elements : " + str (res))
|
Output
The original list is : [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
Rows with list elements : [[2, 1, 8], [6, 1, 2]]
Time complexity: O(n*m), where n is the number of rows and m is the maximum length of a row in the test_list.
Auxiliary space: O(1), as we are not using any extra data structure except for the res list to store the result.
Method #5: Using set.issubset() and list comprehension
Approach:
- Initialize the original list “test_list”.
- Print the original list using the “print()” function.
- Initialize the sublist that we want to check for as “sub_list”.
- Initialize an empty list “res” to store the rows that have all the elements of the sublist.
- Use list comprehension to check if each row in the original list is a subset of the sublist. If a row is a subset of the sublist, append it to “res”.
- Print the list of rows that have all the elements of the sublist using the “print()” function.
Python3
test_list = [[ 7 , 6 , 3 , 2 ], [ 5 , 6 ], [ 2 , 1 , 8 ], [ 6 , 1 , 2 ]]
print ( "The original list is : " + str (test_list))
sub_list = [ 1 , 2 ]
sub_set = set (sub_list)
res = []
for row in test_list:
if sub_set.issubset( set (row)):
res.append(row)
print ( "Rows with list elements : " + str (res))
|
Output
The original list is : [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
Rows with list elements : [[2, 1, 8], [6, 1, 2]]
Time complexity: O(n^2), where “n” is the number of elements in the original list.
Auxiliary space: O(m), where “m” is the number of rows in the original list that have all the elements of the sublist.
Method #6: Using map() and set() functions
Steps:
- Initialize the given list of lists test_list with the provided elements.
- Initialize the sub-list to find sub_list with the given values [1, 2].
- Use the built-in map() function to convert each inner list of test_list into a set.
- Use the built-in set() function to convert the sub_list into a set.
- Use the filter() function with a lambda function that checks if the set of sub_list is a subset of the set of the current row being filtered.
- Convert the filtered rows back into a list using the list() function.
- Print the final list of rows that contain all elements of the sub_list.
Python3
test_list = [[ 7 , 6 , 3 , 2 ], [ 5 , 6 ], [ 2 , 1 , 8 ], [ 6 , 1 , 2 ]]
print ( "The original list is : " + str (test_list))
sub_list = [ 1 , 2 ]
res = list ( filter ( lambda row: set (sub_list).issubset( set (row)), test_list))
print ( "Rows with list elements : " + str (res))
|
Output
The original list is : [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
Rows with list elements : [[2, 1, 8], [6, 1, 2]]
Time complexity: O(NM), where N is the number of rows in the list and M is the length of the longest row.
Auxiliary space: O(NM), where N is the number of rows in the list and M is the length of the longest row.
Last Updated :
01 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...