Open In App

Python Program that prints rows from the matrix that have same element at a given index

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Matrix, the following article shows how rows which has similar digit at the specified index will be extracted and returned as output.

Input : test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], 
                    [98, 34, 23]], 
                    K = 1 
Output : [[3345, 6355, 83, 938], [192, 993, 49]] 
Explanation : 3 and 9 [ same ] in 1st column.
Input : test_list = [[3445, 6355, 83, 938], [323, 923, 845], [192, 993, 49], 
                    [98, 34, 23]], 
                    K = 1 
Output : [[192, 993, 49]] 
Explanation : 9 in 1st column. 

Method 1 : Using all() and list comprehension

In this, we check for all the digits at a specified index using all(), and list comprehension is used for iterating each row.

Python3




# Initializing list
test_list = [[3345, 6355, 83, 938], [
    323, 923, 845], [192, 993, 49], [98, 34, 23]]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 1
 
# Checking for all elements match using all()
res = [row for row in test_list if all(
    str(i)[K] == str(row[0])[K] for i in row)]
 
# Printing result
print("Filtered Rows : " + str(res))


Output

The original list is : [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]]
Filtered Rows : [[3345, 6355, 83, 938], [192, 993, 49]]

Time Complexity: O(n*m)
Auxiliary Space: O(k)

Method 2 : Using filter() ,lambda and all()

In this, we check for all the rows for similar K column digits, filtering using filter() and lambda function. Similar to above method, all() does the task for checking each element.

Python3




# Initializing list
test_list = [[3345, 6355, 83, 938], [
    323, 923, 845], [192, 993, 49], [98, 34, 23]]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing K
K = 1
 
# Checking for all elements match using all()
# filter() and lambda function performing filtering
res = list(filter(lambda row: all(
    str(i)[K] == str(row[0])[K] for i in row), test_list))
 
# Printing result
print("Filtered Rows : " + str(res))


Output

The original list is : [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]]
Filtered Rows : [[3345, 6355, 83, 938], [192, 993, 49]]

Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in filter() ,lambda and all() which all has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space other than the input list itself. 

Method 3: Using a nested for loop and an if statement

Python3




# initialize test_list
test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]]
# printing original list
print("The original list is : " + str(test_list))
# initialize K
K = 1
 
# initialize an empty list for the result
result = []
 
# loop through each row in the test_list
for row in test_list:
   
  # initialize a flag to keep track of whether all elements in the row match
  match = True
   
  # loop through each element in the row
  for i in row:
     
    # compare the Kth digit of the current element with the Kth digit of the first element in the row
    if str(i)[K] != str(row[0])[K]:
       
      # if they don't match, set the flag to False and break out of the loop
      match = False
      break
   
  # if the flag is still True after looping through all elements, then all elements in the row match
  if match:
    result.append(row)
 
# print the filtered rows
print("Filtered Rows: ", result)
#This code is contributed by Vinay Pinjala.


Output

The original list is : [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49], [98, 34, 23]]
Filtered Rows:  [[3345, 6355, 83, 938], [192, 993, 49]]

Time complexity: O(mn), where m is the number of rows in the test_list and n is the number of elements in each row. This is because for each row, we loop through all elements in the row and check the Kth digit of each element. The all() function also takes O(n) time, so the overall time complexity is O(mn).
Auxiliary space: O(m), because we store the filtered rows in a new list, and the size of the new list is proportional to the number of rows that match the criteria. The space complexity is O(m) because we are storing m filtered rows.

Method 4: Using map() and zip() functions

Python3




# Initialize test_list
test_list = [[3345, 6355, 83, 938], [323, 923, 845], [192, 993, 49],
             [98, 34, 23]]
 
# Initialize K
K = 1
 
# Initialize an empty list for the result
result = []
 
# Looping through each row in the test_list
for row in test_list:
   
  # Extracting Kth digit from each element in the row
  kth_digits = list(map(lambda x: str(x)[K], row))
   
  # Checking if all Kth digits match with
  # the first element in the row
  if all(kth_digit == str(row[0])[K] for kth_digit in kth_digits):
    result.append(row)
 
# Printing filtered rows
print("Filtered Rows: ", result)


Output

Filtered Rows:  [[3345, 6355, 83, 938], [192, 993, 49]]

Time complexity: O(n*m), where n is the number of rows and m is the length of the longest row.
Auxiliary space: O(k), where k is the length of the longest row.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads