Python - Check Similar elements in Matrix rows
Last Updated :
04 Apr, 2023
Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List.
Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Output : True
Explanation : All rows have same elements.
Input : test_list = [[1, 1, 4], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Output : False
Explanation : All rows don't have same elements.
Method #1: Using loop
In this, we iterate through each row and check for its similar number in the list, if all the elements in a row are the same as an ith element in the List, true is returned, else false.
step-by-step approach :
- Initialize a matrix with some values using a nested list. The matrix is assigned to the variable test_list.
- Print the original matrix using the print() function.
- Initialize a list with some target values. The list is assigned to the variable tar_list.
- Initialize a boolean variable res to True which is used to store the result of the comparison.
- Initialize an integer variable flag to 0 which is used to break out of the outer loop if a mismatch is found.
- Loop through each index in the range of the length of the test_list. The variable idx is used to hold the current index value.
- Within the outer loop, loop through each element in the current row of test_list[idx]. The variable ele is used to hold the current element value.
- Check if the current element value is equal to the corresponding index element value in tar_list. If they are not equal, set res to False, set flag to 1, and break out of the inner loop.
- If flag is set to 1 (meaning a mismatch was found), break out of the outer loop as well.
- After the loops have completed, print the result of the comparison using the print() function.
Python3
# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using loop
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
[3, 3, 3], [5, 5, 5, 5]]
# printing original list
print("The original list is : " + str(test_list))
# initializing tar_list
tar_list = [1, 4, 3, 5]
res = True
flag = 0
for idx in range(len(test_list)):
for ele in test_list[idx]:
# checking for row index
# equal to list index elements
if ele != tar_list[idx]:
res = False
flag = 1
break
if flag:
break
# printing result
print("Are row index element similar\
to list index element ? : " + str(res))
Output:
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Are row index element similar to list index element ? : True
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method #2 : Using all() + generator expression
In this, we check for all elements to be equal using all(), and check for all elements follow this pattern using all() again. The iteration of elements and rows is done using generator expression.
Example:
Python3
# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using all() + generator expression
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
[3, 3, 3], [5, 5, 5, 5]]
# printing original list
print("The original list is : " + str(test_list))
# initializing tar_list
tar_list = [1, 4, 3, 5]
# nested all() used to check each element and rows
res = all(all(ele == tar_list[idx] for ele in test_list[idx])
for idx in range(len(test_list)))
# printing result
print("Are row index element\
similar to list index element ? : " + str(res))
Output:
The original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Are row index element similar to list index element ? : True
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #3 : Using count() and len() methods
Python3
# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using loop
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
[3, 3, 3], [5, 5, 5, 5]]
# printing original list
print("The original list is : " + str(test_list))
# initializing tar_list
tar_list = [1, 4, 3, 5]
cnt = 0
res = False
for i in range(0, len(tar_list)):
if(test_list[i].count(tar_list[i]) == len(test_list[i])):
cnt += 1
if(cnt == len(test_list)):
res = True
# printing result
print("Are row index element similar to list index element ? : " + str(res))
OutputThe original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element ? : True
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), no space is required
Method #4 : Using operator.countOf() method
Python3
# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using loop
import operator as op
# initializing Matrix
test_list = [[1, 1, 1], [4, 4],
[3, 3, 3], [5, 5, 5, 5]]
# printing original list
print("The original list is : " + str(test_list))
# initializing tar_list
tar_list = [1, 4, 3, 5]
cnt = 0
res = False
for i in range(0, len(tar_list)):
if(op.countOf(test_list[i], tar_list[i]) == len(test_list[i])):
cnt += 1
if(cnt == len(test_list)):
res = True
# printing result
print("Are row index element similar to list index element ? : " + str(res))
OutputThe original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element ? : True
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Method #5: Using set intersection
- Initialize Matrix and target list as given in the problem statement.
- Iterate through each row in the matrix.
- Convert each row into a set using the set() method.
- Find the intersection of the set and target list using the & operator.
- Check if the length of the intersection is equal to the length of the target list. If yes, then the elements in the row are similar to the target list.
- If all rows pass the similarity test, set the result variable to True, else set it to False.
- Print the result.
Example:
Python3
# Python3 code to demonstrate working of
# Similar all elements as List in Matrix rows
# Using set intersection
# initializing Matrix and target list
test_list = [[1, 1, 1], [4, 4],
[3, 3, 3], [5, 5, 5, 5]]
tar_list = [1, 4, 3, 5]
# printing original list
print("The original list is : " + str(test_list))
res = True
for row in test_list:
if len(set(row) & set(tar_list)) != len(tar_list):
res = False
break
# printing result
print("Are row index element similar to list index element? : " + str(res))
OutputThe original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element? : False
Time complexity: O(n*m), where n is the number of rows and m is the maximum length of a row in the matrix.
Auxiliary space: O(m), where m is the maximum length of a row in the matrix.
Method 6: Using map() and zip() functions
Step-by-step approach:
- Use the map() function to iterate over each row in the test_list
- Use the zip() function to iterate over the elements in the row and their corresponding elements in the tar_list
- Use the all() function to check if all elements in the row are equal to their corresponding elements in the tar_list
- If any row does not meet the condition, set res to False and break out of the loop
- Print the result
Python3
# initializing Matrix
test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
# printing original list
print("The original list is : " + str(test_list))
# initializing tar_list
tar_list = [1, 4, 3, 5]
res = True
for row, tar in zip(test_list, tar_list):
if not all(map(lambda x, y: x == tar, row, [tar]*len(row))):
res = False
break
# printing result
print("Are row index element similar to list index element? : " + str(res))
OutputThe original list is : [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]]
Are row index element similar to list index element? : True
Time complexity: O(n*m), where n is the number of rows in the matrix and m is the length of the largest row
Auxiliary space: O(m), as we are only storing the tar_list for comparison.
Similar Reads
Python - Similar index elements Matrix
Sometimes, while working with data, we can have a problem in which we need to perform the construction of matrix from lists element vertically than horizontally. This kind of application can come in Data Science domains in which we need to construct Matrix from several lists. Lets discuss certain wa
7 min read
Python | Remove similar element rows in tuple Matrix
Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + all() This ta
6 min read
Python - Group similar elements into Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are the same. This kind of problem can have applications in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [1, 3, 4, 4,
8 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
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 - Flag None Element Rows in Matrix
Given a Matrix, return True for rows which contain a None Value, else return False. Input : test_list = [[2, 4, None, 3], [3, 4, 1, None], [2, 4, 7, 4], [2, 8, None]] Output : [True, True, False, True] Explanation : 1, 2 and 4th index contain None occurrence. Input : test_list = [[2, 4, None, 3], [3
4 min read
Python - Remove Rows for similar Kth column element
Given a Matrix, remove row if similar element has occurred in row above in Kth column. Input : test_list = [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]], K = 2 Output : [[3, 4, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]] Explanation : In [2, 3, 5], we already has list [3, 4, 5] having 5 at K, i
7 min read
Python | Extract similar index elements
Sometimes, while working with Python data, we can have a problem in which we require to extract the values across multiple lists which are having similar index values. This kind of problem can come in many domains. Let's discuss certain ways in which this problem can be solved. Method #1 : Using loo
6 min read
Python - Group Records on Similar index elements
Sometimes, while working with Python records, we can have a problem in which we need to perform grouping of particular index of tuple, on basis of similarity of rest of indices. This type of problems can have possible applications in Web development domain. Let's discuss certain ways in which this t
6 min read
Python - Pair elements with Rear element in Matrix Row
Sometimes, while working with data, we can have a problem in which we need to pair each element in container to a specific index element, like rear element. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using lis
7 min read