Find the maximum element of each row in a matrix using Python

In this article, we will learn Python programs to find the maximum element of each row in a matrix. We'll explore three efficient approaches to solve this common problem.

Assume we have taken an NxN input matrix. We will now find the maximum element of each row in an input matrix using the below methods.

Methods Used

The following are the various methods to accomplish this task ?

  • Using Nested For Loops

  • Using max() function

  • Using map() and lambda functions

Method 1: Using Nested For Loops

Algorithm

Following are the steps to find the maximum element of each row ?

  • Create a function to iterate through each row of the matrix

  • For each row, initialize a maximum variable

  • Compare each element in the row with the current maximum

  • Update the maximum if a larger element is found

  • Print the maximum element for each row

Example

The following program returns the maximum element of each row using nested for loops ?

# Function to get the maximum element in each row of a matrix
def maximumRowElement(inputMatrix):
   # Getting the number of rows and columns
   rows = len(inputMatrix)
   cols = len(inputMatrix[0])
   
   # Traversing through each row
   for p in range(rows):
      # Initialize maximum with the first element of the row
      maximum = inputMatrix[p][0]
      
      # Compare with all elements in the current row
      for q in range(1, cols):
         if inputMatrix[p][q] > maximum:
            maximum = inputMatrix[p][q]
      
      # Print the maximum element of current row
      print(f"Row {p+1} maximum: {maximum}")

# Input matrix (4x4 matrix)
inputMatrix = [[5, 1, 3, 4],
               [6, 10, 8, 11],
               [7, 2, 4, 3],
               [1, 2, 3, 4]]

print("The Given Matrix is:")
for row in inputMatrix:
   for element in row:
      print(element, end=' ')
   print()

print("\nMaximum element in each row:")
maximumRowElement(inputMatrix)

The output of the above code is ?

The Given Matrix is:
5 1 3 4 
6 10 8 11 
7 2 4 3 
1 2 3 4 

Maximum element in each row:
Row 1 maximum: 5
Row 2 maximum: 11
Row 3 maximum: 7
Row 4 maximum: 4

Method 2: Using max() Function

The max() function returns the highest-valued item in an iterable. This provides a cleaner and more efficient solution.

Example

The following program uses the built-in max() function ?

# Input matrix (4x4 matrix)
inputMatrix = [[5, 1, 3, 4],
               [6, 10, 8, 11],
               [7, 2, 4, 3],
               [1, 2, 3, 4]]

print("The Given Matrix is:")
for row in inputMatrix:
   for element in row:
      print(element, end=' ')
   print()

print("\nMaximum element in each row:")
# Using max() function for each row
for i, row in enumerate(inputMatrix):
   print(f"Row {i+1} maximum: {max(row)}")
The Given Matrix is:
5 1 3 4 
6 10 8 11 
7 2 4 3 
1 2 3 4 

Maximum element in each row:
Row 1 maximum: 5
Row 2 maximum: 11
Row 3 maximum: 7
Row 4 maximum: 4

Method 3: Using map() and Lambda Functions

The map() function applies a specified function to each element of an iterable. Combined with lambda functions, it provides a functional programming approach.

Example

The following program uses map() with lambda to find maximum elements ?

# Input matrix (4x4 matrix)
inputMatrix = [[5, 1, 3, 4],
               [6, 10, 8, 11],
               [7, 2, 4, 3],
               [1, 2, 3, 4]]

print("The Given Matrix is:")
for row in inputMatrix:
   for element in row:
      print(element, end=' ')
   print()

print("\nMaximum element in each row:")
# Apply max() function to each row using map() and lambda
result = list(map(lambda row: max(row), inputMatrix))

# Display results
for i, max_val in enumerate(result):
   print(f"Row {i+1} maximum: {max_val}")
The Given Matrix is:
5 1 3 4 
6 10 8 11 
7 2 4 3 
1 2 3 4 

Maximum element in each row:
Row 1 maximum: 5
Row 2 maximum: 11
Row 3 maximum: 7
Row 4 maximum: 4

Comparison

Method Code Complexity Readability Best For
Nested Loops High Good for beginners Understanding logic
max() function Low Very clear Most practical cases
map() + lambda Medium Functional style Functional programming

Conclusion

The max() function provides the most efficient and readable solution for finding maximum elements in matrix rows. Use nested loops for learning purposes and map() with lambda for functional programming approaches.

Updated on: 2026-03-26T23:50:51+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements