Editor

Python Program to Rotate a Matrix by 90 Degrees

Intermediate · Matrices

Rotating a square matrix 90 degrees clockwise turns it a quarter turn to the right, so the first column, read from the bottom up, becomes the first row. Rotating an image works the same way, since an image is a grid of pixels. This program builds the rotated matrix with nested loops and prints both, row by row.

Program

rotate_matrix.py
def show(title, matrix):
print(title)
for row in matrix:
for value in row:
print(f"{value:4}", end="")
print()
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
n = len(matrix)
# Clockwise: the entry at row i, column j moves to row j, column n - 1 - i
rotated = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
rotated[j][n - 1 - i] = matrix[i][j]
show("Original matrix:", matrix)
show("Rotated 90 degrees clockwise:", rotated)
Output
Original matrix:
   1   2   3   4
   5   6   7   8
   9  10  11  12
  13  14  15  16
Rotated 90 degrees clockwise:
  13   9   5   1
  14  10   6   2
  15  11   7   3
  16  12   8   4

How it works

  1. rotated starts as an n x n matrix of zeros; the list comprehension gives each row its own list.
  2. In a clockwise turn, row i of the original becomes column n - 1 - i of the result: the top row becomes the right-hand column.
  3. So the entry at matrix[i][j] is copied to rotated[j][n - 1 - i]. For example 1, at row 0, column 0, moves to row 0, column 3.
  4. show() prints a title and the matrix row by row, giving every number four spaces so the columns line up.

Other ways to write it

In place: transpose, then reverse each row

In place: transpose, then reverse each row
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
n = len(matrix)
for i in range(n): # 1. transpose: swap across the diagonal
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
for row in matrix: # 2. mirror each row
row.reverse()
print("Rotated clockwise, in place:")
for row in matrix:
print(row)
Output
Rotated clockwise, in place:
[7, 4, 1]
[8, 5, 2]
[9, 6, 3]

A transpose followed by reversing every row is the same as a clockwise turn, and it needs no second matrix. This is the usual interview answer.

Using zip() for both directions

Using zip() for both directions
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
clockwise = [list(row) for row in zip(*matrix[::-1])]
anticlockwise = [list(row) for row in zip(*matrix)][::-1]
print("Clockwise:")
for row in clockwise:
print(row)
print("Anticlockwise:")
for row in anticlockwise:
print(row)
Output
Clockwise:
[7, 4, 1]
[8, 5, 2]
[9, 6, 3]
Anticlockwise:
[3, 6, 9]
[2, 5, 8]
[1, 4, 7]

Reversing the rows and then taking the columns turns the matrix clockwise; taking the columns and then reversing their order turns it anticlockwise.

Try it yourself

Rotate the matrix twice and check that the result is the original turned upside down, 180 degrees.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises