Interchange elements of the first and last rows in the matrix using Python


In this article, we will learn a python program to interchange elements of the first and last rows in a matrix.

Methos Used

  • Using Temporary Variable(Using Nested Loops)

  • Using Swapping(“,”) Operator(Without Using Any Loops)

  • Using pop(),insert() & append() functions

Method 1: Using Temporary Variable(Using Nested Loops)

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Create a function swapFirstandLastRow() to swap the first and last rows of an input matrix by accepting the input matrix, rows, and columns as arguments.

  • Traverse through the matrix rows and columns and Swap the first and last row elements using the temporary variable.

  • Create a variable to store the input matrix. Create a variable to store the input number of rows of a matrix.

  • Create another variable to store the input number of columns of a matrix.

  • Call the above-defined swapFirstandLastRow() function by passing the input matrix, rows, and columns to it to swap the first and last rows.

  • Print the resultant matrix after interchanging the first and last rows of an input matrix by traversing through rows and columns using nested for loop.

Example

The following program interchange the first and last rows of an input matrix using Temporary Variable and Nested Loops −

# function to swap the first and last rows of an input matrix
# by accepting input matrix, rows, and columns as arguments
def swapFirstandLastRow(inputMatrix, rows, cols):
   # Swapping first and last row elements using temp variable
      for p in range(rows):
         temp = inputMatrix[0][p]
         inputMatrix[0][p] = inputMatrix[rows-1][p]
         inputMatrix[rows-1][p] = temp
# input matrix
inputMatrix = [[5, 1, 3],
               [9, 6, 8],
               [4, 2, 7]]
# input no of rows of a matrix
rows = 3
# input no of columns of a matrix
cols = 3
# calling the above-defined swapFirstandLastRow() function
# by passing input matrix, rows, and columns to it
swapFirstandLastRow(inputMatrix, rows, cols)
print("Matrix after interchanging first and last rows:")
# traversing through the rows of a matrix
for p in range(rows):
   # Traversing through the columns of a current row
   for q in range(cols):
      #printing the corresponding element at the current row and column of the matrix
      print(inputMatrix[p][q], end=" ")
   # Printing a new line
   print()

Output

On execution, the above program will generate the following output −

Matrix after interchanging first and last rows:
4 2 7 
9 6 8 
5 1 3 

Method 2: Using Swapping(“,”) Operator(Without Using Any Loops)

Example

The following program interchange the first and last rows of an input matrix without using any loop and (,) operator −

# input matrix
inputMatrix = [[5, 1, 3],
               [9, 6, 8],
               [4, 2, 7]]
# Swapping first and last rows using the (,) operator
inputMatrix[0], inputMatrix[-1] = inputMatrix[-1], inputMatrix[0]
# input no of rows of a matrix
rows = 3
# input no of columns of a matrix
cols = 3
print("Matrix after interchanging first and last rows:")
# traversing through the rows of a matrix
for p in range(rows):
   # Traversing through the columns of a current row
  for q in range(cols):
     # Printing the corresponding element at the current row & column
     print(inputMatrix[p][q], end=" ")
  print()

Output

On execution, the above program will generate the following output −

Matrix after interchanging first and last rows:
4 2 7 
9 6 8 
5 1 3 

Method 3: Using pop(),insert() & append() functions

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

  • Use the negative indexing to get the last row of a matrix.

  • Use the positive indexing to get the first row of a matrix.

  • Use the pop() function(removes the last element from a list and returns it) to remove the last row of a matrix.

  • Use the pop() function by passing the index as 0 to remove the first row of a matrix.

  • Use the insert() function(inserts the value at the specified index) to insert the last row at index 0(first row).

  • Use the append() function(adds the element to the list at the end) to append the first row of a matrix at the end.

  • Print the resultant matrix after interchanging the first and last rows of an input matrix by traversing through rows and columns using nested for loop.

Example

The following program interchange the first and last rows of an input matrix using pop(), insert() & append() functions −

# input matrix
inputMatrix = [[5, 1, 3],
               [9, 6, 8],
               [4, 2, 7]]
# Getting the last row of a matrix
last_row = inputMatrix[-1]
# Getting the first row of a matrix
first_row = inputMatrix[0]
# Removing the last row of a matrix
inputMatrix.pop()
# Removing the first row of a matrix
inputMatrix.pop(0)
# Inserting the last row at the index 0(first row)
inputMatrix.insert(0, last_row)
# Appending(Adding at the end) the first row of a matrix at the end
inputMatrix.append(first_row)
# input no of rows of a matrix
rows = 3
# input no of columns of a matrix
cols = 3
print("Matrix after interchanging first and last rows:")
# traversing through the rows of a matrix
for p in range(rows):
   # Traversing through the columns of a current row
   for q in range(cols):
      #printing the corresponding element at the current row and column of the matrix
      print(inputMatrix[p][q], end=" ")
   print()

Output

On execution, the above program will generate the following output −

Matrix after interchanging first and last rows:
4 2 7 
9 6 8 
5 1 3 

Conclusion

We learned how to swap the first and last elements of the matrix using three distinct approaches in this article: Using a temporary variable, the (,) operator, and the methods like pop() and insert(). We also learned how to use the pop() and insert() functions to remove and add (insert) rows to the matrix. We learned how to swap two variables/iterables with the (,) operator.

Updated on: 27-Jan-2023

802 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements