Python Program to Interchange Elements of First and Last in a Matrix Across Rows

A matrix is a set of numbers arranged in rows and columns format. In Python, a matrix can be created using nested lists or NumPy arrays. This tutorial demonstrates how to interchange the first and last rows of a matrix.

Input Output Scenarios

Let's look at examples of interchanging first and last rows in different matrices ?

Input matrix: 
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Output matrix: 
[7, 8, 9]
[4, 5, 6]
[1, 2, 3]

For matrices with unequal row lengths ?

Input matrix: 
['a', 'b']
['c', 'd', 'e']
['f', 'g', 'h', 'i']

Output matrix: 
['f', 'g', 'h', 'i']
['c', 'd', 'e']
['a', 'b']

Method 1: Using Row Swapping

The simplest approach is to directly swap the first and last rows using indexing ?

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

# Function for displaying matrix
def display(matrix):
    for row in matrix:
        print(row)
    print()

# Display original matrix
print("Original matrix:")
display(matrix)

# Interchange first and last rows
matrix[0], matrix[-1] = matrix[-1], matrix[0]

# Display changed matrix
print("Changed matrix:")
display(matrix)
Original matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Changed matrix:
[7, 8, 9]
[4, 5, 6]
[1, 2, 3]

Example with Unequal Dimensions

This method works equally well for matrices with rows of different lengths ?

matrix = [['a', 'b'], ['c', 'd', 'e'], [1, 2, 3]]

# Function for displaying matrix
def display(matrix):
    for row in matrix:
        print(row)
    print()

# Display original matrix
print("Original matrix:")
display(matrix)

# Interchange first and last rows
matrix[0], matrix[-1] = matrix[-1], matrix[0]

# Display changed matrix
print("Changed matrix:")
display(matrix)
Original matrix:
['a', 'b']
['c', 'd', 'e']
[1, 2, 3]

Changed matrix:
[1, 2, 3]
['c', 'd', 'e']
['a', 'b']

Method 2: Using List Methods (pop, insert, append)

An alternative approach uses Python's list manipulation methods ?

  • pop() Removes element at specified position (last by default)
  • insert() Inserts element at specified position
  • append() Adds element at the end of the list
matrix = [['a', 'b'], ['c', 'd', 'e'], ['f', 'g', 'h', 'i']]

# Function for displaying matrix
def display(matrix):
    for row in matrix:
        print(row)
    print()

# Display original matrix
print("Original matrix:")
display(matrix)

# Store first and last rows
first_row = matrix[0]
last_row = matrix[-1]

# Remove first and last rows
matrix.pop(0)  # Remove first row
matrix.pop()   # Remove last row

# Insert last row at beginning and append first row at end
matrix.insert(0, last_row)
matrix.append(first_row)

# Display changed matrix
print("Changed matrix:")
display(matrix)
Original matrix:
['a', 'b']
['c', 'd', 'e']
['f', 'g', 'h', 'i']

Changed matrix:
['f', 'g', 'h', 'i']
['c', 'd', 'e']
['a', 'b']

Comparison

Method Complexity Readability Best For
Row Swapping O(1) High Simple interchange
List Methods O(n) Medium Complex manipulations

Conclusion

Row swapping using indexing is the most efficient method for interchanging first and last rows. The list methods approach is useful when you need more complex matrix manipulations beyond simple row swapping.

Updated on: 2026-03-27T06:38:41+05:30

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements