Python | Row with Minimum element in Matrix
Last Updated :
08 May, 2023
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 in web development. Let's discuss certain ways in which this task can be performed.
Method #1 : Using reduce() + lambda The above two function can help us achieving this particular task. The lambda function does the task of logic and iteration and reduce function does the task of returning the required result. Works in python 2 only.
Python3
# Python code to demonstrate
# Row with Minimum element in Matrix
# using reduce() + lambda
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
# using reduce() + lambda
# Row with Minimum element in Matrix
res = reduce(lambda i, j: i if min(i) < min(j) else j, test_matrix)
# printing result
print("Minimum element sublist is : " + str(res))
Output : The original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]
Time Complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(m)
Method #2: Using min() + key The min function can get the minimum of all the list and key is used to specify on what the min condition has to be applied that is on rows in this case.
Python3
# Python3 code to demonstrate
# Row with Minimum element in Matrix
# using min() + key
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
# using min() + key
# Row with Minimum element in Matrix
res = min(test_matrix, key=min)
# printing result
print("Minimum element sublist is : " + str(res))
Output : The original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3 : Using min() and extend() methods
Python3
# Python code to demonstrate
# Row with Minimum element in Matrix
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
# printing the original matrix
print ("The original matrix is : " + str(test_matrix))
# Row with Minimum element in Matrix
a=[]
for i in test_matrix:
a.extend(i)
mi=min(a)
for i in test_matrix:
if mi in i:
res=i
break
# printing result
print ("Minimum element sublist is : " + str(res))
OutputThe original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]
Time Complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(k), k is the length of the list
Method #4: Using numpy
Step-by-step algorithm:
- Import the numpy library.
- Initialize the matrix.
- Find the index of the row with the minimum element using the argmin() function of the numpy library.
- Get the corresponding row from the matrix.
- Print the result.
Python3
# importing the numpy library
import numpy as np
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
# printing the original matrix
print ("The original matrix is : " + str(test_matrix))
# using the argmin() function of the numpy library to find the index of the row with the minimum element
min_index = np.argmin(test_matrix)
# getting the corresponding row from the matrix
min_row = test_matrix[min_index]
# printing the result
print ("Minimum element sublist is : " + str(min_row))
#This code is contributed by Vinay Pinjala.
OutputThe original matrix is : [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
Minimum element sublist is : [1, 3, 1]
Time complexity: The argmin() function takes O(N) time to find the minimum element and the corresponding row is obtained in O(1) time. Therefore, the overall time complexity is O(N).
Auxiliary Space: The space complexity is O(1) since no extra space is used to store the matrix.
METHOD 5:Using nested loop
APPROACH:
The given Python code finds the sublist with the minimum element from a given matrix.
ALGORITHM:
1. Assign the first sublist of the matrix to min_sublist.
2. Iterate over each sublist in the matrix using a for loop.
3. For each sublist, iterate over each element using another for loop.
4. If the current element is smaller than the minimum element of min_sublist, update min_sublist to the current sublist.
5. After iterating over all sublists and elements, min_sublist will contain the sublist with the minimum element.
6. Print the minimum element sublist.
Python3
matrix = [[1, 3, 1], [4, 5, 3], [7, 2, 4]]
min_sublist = matrix[0]
for sublist in matrix:
for element in sublist:
if element < min(min_sublist):
min_sublist = sublist
print("Minimum element sublist is :", min_sublist)
OutputMinimum element sublist is : [1, 3, 1]
Time Complexity: O(n^2), where n is the number of sublists in the matrix. This is because the code iterates over each element in each sublist using nested for loops.
Space Complexity: O(n), where n is the number of elements in the smallest sublist. This is because min_sublist is assigned the first sublist of the matrix, and then updated to the sublist with the minimum element. Therefore, the maximum size of min_sublist is the size of the smallest sublist.
Similar Reads
Python - Row with Minimum Sum 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 having shorthands to perform the same are always helpful as this kind of problem can come in web development. Method #1 : Using reduce() + lambda The
4 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
Python | Minimum element in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the minimum element of all the records received. This is a very common application that can occur in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 :
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 - Minimum element indices
Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of minimum element of list. This task is easy and discussed many times. But sometimes, we can have multiple minimum elements and hence multiple minimum positions. Letâs discuss a shorthand to ac
3 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 program to Sort Matrix by Maximum Row element
Given a Matrix, sort rows by maximum element. Input : test_list = [[5, 7, 8], [9, 10, 3], [10, 18, 3], [0, 3, 5]] Output : [[10, 18, 3], [9, 10, 3], [5, 7, 8], [0, 3, 5]] Explanation : 18, 10, 8 and 5 are maximum elements in rows, hence sorted. Input : test_list = [[9, 10, 3], [10, 18, 3], [0, 3, 5]
4 min read
Python - Check Similar elements in Matrix rows
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,
8 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 - Dual Element row with Maximum difference
Sometimes, while working with Python Matrix, we can have Matrix with its elements to be rows with just two elements and we may desire to get row with elements having maximum difference. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #
6 min read