- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 [0] 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[0], matrix[-1] = matrix[-1], matrix[0] 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[0], 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[0], matrix[-1] = matrix[-1], matrix[0] # 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[0] 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.