rotate matrix 90 degrees clockwise python 1

rotate matrix 90 degrees clockwise python

#rotate 90 deg clockwise
box=[["a","b"],["c","d"],["e","f"]]

rows = len(box)
cols = len(box[0])

box2 = [[""] * rows for _ in range(cols)]

for x in range(rows):
    for y in range(cols):
        box2[y][rows - x - 1] = box[x][y]

Here is what the above code is Doing:
1. Create a new list of lists, with the same number of columns as the original list of lists, and the same number of rows as the original list of lists’ number of columns.
2. Iterate through the original list of lists, and for each element, insert it into the new list of lists at the position that is the original element’s column number, and the new list of lists’ number of rows minus the original element’s row number minus 1.

Similar Posts