Python - Rows with all List elements
Last Updated :
01 May, 2023
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
# Python3 code to demonstrate working of
# Rows with all List elements
# Using loop
# initializing list
test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing list
sub_list = [1, 2]
res = []
for row in test_list:
flag = True
# checking for all elements in list
for ele in sub_list:
if ele not in row:
flag = False
if flag:
res.append(row)
# printing result
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
# Python3 code to demonstrate working of
# Rows with all List elements
# Using all() + list comprehension
# initializing list
test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing list
sub_list = [1, 2]
# testing elements presence using all()
res = [row for row in test_list if all(ele in row for ele in sub_list)]
# printing result
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
# Python3 code to demonstrate working of
# Rows with all List elements
# Using nested loops
# initializing list
test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing list
sub_list = [1, 2]
# initializing result list
res = []
# looping through the rows in the test_list
for row in test_list:
# initializing a boolean variable to check if all elements in sub_list are in the row
is_present = True
# looping through the elements in sub_list
for ele in sub_list:
# if the element is not in the row, set is_present to False and break out of the loop
if ele not in row:
is_present = False
break
# if all elements in sub_list are present in the row, add the row to the result list
if is_present:
res.append(row)
# printing result
print("Rows with list elements : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Rows with all List elements
# Using set intersection
# initializing list
test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing list
sub_list = [1, 2]
sub_set = set(sub_list)
# initializing result list
res = []
# iterating through each row of test_list
for row in test_list:
# converting the row to a set
row_set = set(row)
# taking the intersection of row_set and sub_set
intersection = row_set & sub_set
# if all elements of sub_list are present in the row, append the row to res
if len(intersection) == len(sub_set):
res.append(row)
# printing result
print("Rows with list elements : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Rows with all List elements
# Using set and all() function
# initializing list
test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing sublist and set
sub_list = [1, 2]
sub_set = set(sub_list)
# initializing result list
res = []
# iterating through each row of test_list
for row in test_list:
# checking if sub_set is a subset of the set of elements in row
if sub_set.issubset(set(row)):
res.append(row)
# printing result
print("Rows with list elements : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Rows with all List elements
# Using map() and set()
# initializing list
test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]]
# printing original list
print("The original list is : " + str(test_list))
# initializing list
sub_list = [1, 2]
# finding rows that contain all elements of sub_list using map() and set()
res = list(filter(lambda row: set(sub_list).issubset(set(row)), test_list))
# printing result
print("Rows with list elements : " + str(res))
OutputThe 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.
Similar Reads
Python - Filter Rows with Range Elements Given a Matrix, filter all the rows which contain all elements in the given number range. Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] Explanation : 2, 3, 4, 5 all are present in above rows. Input :
5 min read
Python - Retain all K elements Rows Sometimes, while working with Python lists, we can have a problem in which we need to retain rows which have only K as elements. This kind of application can occur in data domains which take Matrix as input. Let's discuss certain ways in which this task can be performed. Input : test_list = [[7, 6],
8 min read
Python - Filter rows with required elements Given a Matrix, filter rows with required elements from other list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4, 8], [1, 1, 9]], check_list = [4, 6, 2, 8] Output : [[2, 4, 6], [2, 4, 8]] Explanation : All elements are from the check list. Input : test_list = [[2, 4, 6], [7, 4, 3, 2], [2, 4,
6 min read
Python - Filter rows with Elements as Multiple of K Given a Matrix, extract rows with elements multiple of K. Input : test_list = [[5, 10, 15], [4, 8, 12], [100, 15], [5, 10, 23]], K = 4 Output : [[4, 8, 12]] Explanation : All are multiples of 4. Input : test_list = [[5, 10, 15], [4, 8, 11], [100, 15], [5, 10, 23]], K = 4 Output : [] Explanation : No
6 min read
Python - Remove the row if all elements equal to N Sometimes, while handling data, especially in the Machine Learning domain, we need to go through a lot of similar N-equal data. We sometimes need to eliminate the rows which are all equal to N. Letâs discuss certain ways to remove the rows that have all N values as list columns. Method #1: Using lis
5 min read