5 Best Ways to Interchange Elements of First and Last in a Matrix Across Columns in Python

πŸ’‘ Problem Formulation:

You are given a matrix (a list of lists in Python), and your task is to write a Python program that interchanges the elements of the first row with the last row across their respective columns. If the input matrix is [[1,2,3], [4,5,6], [7,8,9]], the desired output after the operation should be [[7,8,9], [4,5,6], [1,2,3]].

Method 1: Using Indexing

This method involves directly accessing the first and last rows of the matrix using their indices and swapping them. It’s a straightforward and easy-to-read approach that utilizes the power of Python’s list indexing.

Here’s an example:

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

def interchange_rows(mat):
    mat[0], mat[-1] = mat[-1], mat[0]
    return mat

print(interchange_rows(matrix))

Output:

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

This snippet switches the first and last rows of the given matrix by using simple assignment and negative indexing, making it highly readable and efficient for this task.

Method 2: Using Tuple Unpacking

By applying tuple unpacking, we can elegantly swap the rows in a single line of code. This method retains code clarity while avoiding temporary variables.

Here’s an example:

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

def interchange_rows(mat):
    mat[0], mat[-1] = mat[-1], mat[0]
    return mat

print(interchange_rows(matrix))

Output:

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

The code showcases tuple unpacking where mat[0] and mat[-1] are swapped in a single expression, leading to an efficient and Pythonic solution.

Method 3: Using a Function with Slicing

Slicing in Python is a technique for accessing parts of data structures. Here, we use slicing to achieve the row switch in a readable and concise manner within a utility function.

Here’s an example:

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

def interchange_rows(mat):
    mat[:] = mat[-1:] + mat[1:-1] + mat[:1]
    return mat

print(interchange_rows(matrix))

Output:

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

The function alters the matrix by using list slicing to reorder the rows, making it a clean and efficient way to perform the interchange without mutating the original matrix outside the function’s scope.

Method 4: Using pop() and insert()

This approach uses list manipulation functions pop() and insert() to remove the first and last rows from their original positions and insert them into each other’s places.

Here’s an example:

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

def interchange_rows(mat):
    mat.insert(0, mat.pop())
    return mat

print(interchange_rows(matrix))

Output:

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

In this code, pop() removes the last row, and insert() then places it at the beginning, effectively swapping the first and last rows. This method efficiently performs the task while maintaining the original matrix’s order.

Bonus One-Liner Method 5: List Comprehension

List comprehension in Python offers a concise and highly readable way to create lists. Here, it is used to generate a new matrix with the first and last rows interchanged in a single line of code.

Here’s an example:

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

interchange_rows = [matrix[-1]] + matrix[1:-1] + [matrix[0]]

print(interchange_rows)

Output:

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

This code utilizes a list comprehension to create a new matrix with the interchanged rows in just one line. It’s a powerful one-liner that demonstrates the expressiveness of Python.

Summary/Discussion

  • Method 1: Using Indexing. Easy to understand. Swaps elements in-place.
  • Method 2: Using Tuple Unpacking. Pythonic and concise. In-place operation without temporary variables.
  • Method 3: Using a Function with Slicing. Clear and concise. Utilizes slicing to create a new sequence without altering the original matrix outside the function.
  • Method 4: Using pop() and insert(). Manipulates the list directly, slightly less readable due to direct list manipulation but still efficient.
  • Bonus Method 5: List Comprehension. Extremely concise and readable, but creates a new list instead of modifying in place, which might be a concern for large matrices.