Python Program that prints rows from the matrix that have same element at a given index
Last Updated :
15 May, 2023
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))
OutputThe 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))
OutputThe 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.
OutputThe 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)
OutputFiltered 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.
Similar Reads
Python Program that prints the rows of a given length from a matrix
Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
4 min read
Python program to return rows that have element at a specified index
Given two Matrices, the task is to write a Python program that can extract all the rows from both matrices which have similar elements at their Kth index, mapped at similar row positions. Examples: Input : test_list1 = [[1, 8, 3], [9, 2, 0], [6, 4, 4], [6, 4, 4]], test_list2 = [[1, 9, 3], [8, 2, 3],
5 min read
Python Program to Print a given matrix in reverse spiral form
Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input: 1 2
5 min read
Python program to print Rows where all its Elements' frequency is greater than K
Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3
8 min read
Python program to extract rows from Matrix that has distinct data types
Given a Matrix, the task is to write a Python program to extract rows with no repeated data types. Examples: Input : test_list = [[4, 3, 1], ["gfg", 3, {4:2}], [3, 1, "jkl"], [9, (2, 3)]]Output : [['gfg', 3, {4: 2}], [9, (2, 3)]]Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has
6 min read
Python Program to find the Next Nearest element in a Matrix
Given a matrix, a set of coordinates and an element, the task is to write a python program that can get the coordinates of the elements next occurrence. Input : test_list = [[4, 3, 1, 2, 3], [7, 5, 3, 6, 3], [8, 5, 3, 5, 3], [1, 2, 3, 4, 6]], i, j = 1, 3, K = 3 Output : (1, 4) Explanation : After (1
4 min read
Python Program to Find maximum element of each row in a matrix
Given a matrix, the task is to find the maximum element of each row.Examples:Â Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Method 1: The idea is to run the loop for no_of_rows. Check each element inside the row and fi
5 min read
Python Program that prints elements common at specified index of list elements
Given a list of strings, the task is to write a Python program to extract all characters that are same at a specified index of each element of a list. Illustration: Input : test_list = ["geeks", "weak", "beak", "peek"] Output : ['e', 'k'] Explanation : e and k are at same at an index on all strings.
5 min read
Python Program to Extract Rows of a matrix with Even frequency Elements
Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements. Examples: Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]Explanation: frequency of 4-> 4 which is even frequency
5 min read
Python Program to Sort Matrix Rows According to Primary and Secondary Indices
Given Matrix, the task here is to write a Python program to sort rows based on primary and secondary indices. First using primary indices the rows will be arranged based on the element each row has at the specified primary index. Now if two rows have the same element at the given primary index, sort
3 min read