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 - Elements with same index
Given a List, get all elements that are at their index value. Input : test_list = [3, 1, 8, 5, 4, 10, 6, 9]Â Output : [1, 4, 6]Â Explanation : These elements are at same position as its number.Input : test_list = [3, 10, 8, 5, 14, 10, 16, 9]Â Output : []Â Explanation : No number at its index. Method
5 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
Transpose Elements of Two Dimensional List - Python
The task of transposing elements of a two-dimensional list in Python involves converting rows into columns and vice versa. This process rearranges the data structure so that the elements in the same position across different rows are grouped together in new sublists. For example, given the 2D list a
3 min read
Python | Row with Minimum element in Matrix
We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read
Python - Row with Maximum Record Element
Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
7 min read
Search Elements in a Matrix - Python
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
3 min read