Python Program to Reverse Every Kth row in a Matrix

In Python, a matrix is a two-dimensional array where all elements have the same data type. Reversing every Kth row means flipping the order of elements in rows at positions K, 2K, 3K, and so on.

In this article, we will learn different methods to reverse every Kth row in a matrix using Python.

Problem Overview

Given a matrix and a value K, we need to reverse the elements in every Kth row. For example, if K=2, we reverse the 2nd, 4th, 6th rows, etc.

Input

matrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
k = 2
print("Original matrix:", matrix)
Original matrix: [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]

Expected Output

[[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

Here, the 2nd row [3, 10, 6] becomes [6, 10, 3] and the 4th row [8, 6, 1] becomes [1, 6, 8].

Method 1: Using enumerate() and reversed()

The enumerate() function provides both index and element while iterating. We check if the row index is a multiple of K and reverse those rows ?

# Input matrix
matrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
k = 2

result = []
for index, row in enumerate(matrix):
    # Check if current row is Kth row (1-indexed)
    if (index + 1) % k == 0:
        result.append(list(reversed(row)))
    else:
        result.append(row)

print("Result:", result)
Result: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

Method 2: Using List Comprehension and Slicing

List comprehension provides a concise way to create new lists. We use slice [::-1] to reverse rows ?

matrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
k = 2

result = [row[::-1] if (index + 1) % k == 0 else row 
          for index, row in enumerate(matrix)]

print("Result:", result)
Result: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

Method 3: Using range() Function

The range() function can directly target Kth rows by starting at K-1 and stepping by K ?

matrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
k = 2

# Reverse every Kth row starting from index k-1
for i in range(k-1, len(matrix), k):
    matrix[i] = matrix[i][::-1]

print("Result:", matrix)
Result: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

Method 4: Using In-Place Reversal

The reverse() method modifies the original list in-place, saving memory ?

matrix = [[7, 1, 4], [3, 10, 6], [1, 4, 2], [8, 6, 1]]
k = 2

for index, row in enumerate(matrix):
    if (index + 1) % k == 0:
        row.reverse()  # In-place reversal

print("Result:", matrix)
Result: [[7, 1, 4], [6, 10, 3], [1, 4, 2], [1, 6, 8]]

Comparison

Method Memory Usage Modifies Original Best For
enumerate() + reversed() Creates new matrix No Functional programming style
List comprehension Creates new matrix No Concise one-liners
range() + slicing Modifies original Yes Direct index access
In-place reverse() Memory efficient Yes Memory-constrained scenarios

Conclusion

Use range() with slicing for the most efficient approach when modifying the original matrix is acceptable. Use list comprehension for readable, functional-style code when you need to preserve the original matrix.

Updated on: 2026-03-27T13:04:15+05:30

975 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements