Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython – Rows with all List elements

Python – Rows with all List elements

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))


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:

  1. Convert the sub_list to a set for faster intersection operation.
  2. Initialize an empty list, res, to store the rows that contain all the elements of sub_list.
  3. Iterate through each row of the test_list.
  4. Convert the current row to a set.
  5. Take the intersection of the current row set and the sub_list set using the & operator.
  6. 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.
  7. Append the current row to the res list if all the elements of sub_list are present in the current row.
  8. 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))


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:

  1. Initialize the original list “test_list”.
  2. Print the original list using the “print()” function.
  3. Initialize the sublist that we want to check for as “sub_list”.
  4. Initialize an empty list “res” to store the rows that have all the elements of the sublist.
  5. 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”.
  6. 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))


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:

  1. Initialize the given list of lists test_list with the provided elements.
  2. Initialize the sub-list to find sub_list with the given values [1, 2].
  3. Use the built-in map() function to convert each inner list of test_list into a set.
  4. Use the built-in set() function to convert the sub_list into a set.
  5. 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.
  6. Convert the filtered rows back into a list using the list() function.
  7. 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))


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.

RELATED ARTICLES

Most Popular

Recent Comments