# 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 not be created directly. Instead, we can use a nested list or NumPy array as a matrix.

The interchanging of first and last row elements of a matrix is demonstrated below.

## Input Output Scenarios

Assume we have a 3X3 matrix represented using a nested list. and output matrix will be the resultant matrix whose first and last row elements are interchanged.

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

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


Let’s consider another matrix whose rows and columns are unequal.

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

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


Let's discuss the one by one approach −

## Swapping the Rows

We can simply interchange the elements of first and last in a matrix across rows, by swapping the first and last rows.

### Example

In this example, we will swap the first and last rows with the help of first  and last [-1] row indexes.

matrix = [[1,2,3], [4,5,6], [7,8,9]]
#function for displaying matrix
def display(matrix):
for row in matrix:
print(row)
print()

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

# interchanging the element between first and last rows
def swapLines(matrix):
matrix, matrix[-1] = matrix[-1], matrix
return matrix

# displaying changed matrix
print("Changed matrix: ")
display(swapLines(matrix))


### Output

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

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


By using the python positive and negative indexing (matrix, matrix[-1]) we have interchanged the elements of first and last in a matrix across rows successfully.

### Example

In this example, we will interchange the first and last rows of a matrix whose rows and columns dimensions are unequal.

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

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

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

# interchanging the element between first and last rows
matrix, matrix[-1] = matrix[-1], matrix

# displaying changed matrix
print("Changed matrix: ")
display(matrix)


### Output

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

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


In this example the original matrix is not a square matrix and we have successfully interchanged the elements of first and last in a matrix across rows.

## Using Pop(), Insert(), and Append() Methods

In python pop(), insert() and append() methods are list manipulating methods.

• pop()  The pop method removes the element at the specified position. By default, it removes the last element.

• insert()  This method can be used to insert an element at any desired position. This method takes two arguments, one is the element and the index at which the element has to be inserted.

• append()  method is used to add an element at the end of the list.

### Example

With the help of above mentioned methods we will interchange the elements of first and last row of a matrix.

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

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

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

# interchanging the element between first and last rows
temp1 = matrix[-1]
temp2 = matrix
matrix.pop()
matrix.pop(0)
matrix.insert(0, temp1)
matrix.append(temp2)

# displaying changed matrix
print("Changed matrix: ")
display(matrix)


### Output

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

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


By using python list manipulation methods called pop(), insert(), and append() we have successfully interchanged the elements of first and last in a matrix across rows.

Note − matrices are created by using a python list of lists.

Updated on: 15-May-2023

22 Views 