0

How to to make this array rotate by 90 degrees to right without using numpy.

multiarray = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
auxiliaryArray = []
colLength = len(multiarray[0])
rowLength = len(multiarray)
for indexRow in range(len(multiarray)):
    for indexCol in range(len(multiarray[0])):
        auxiliaryArray[indexCol][rowLength - 1 - indexRow] = multiarray[indexRow][indexCol]

print(auxiliaryArray)

Error: IndexError: list index out of range

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

2
  • instead of making an empty list, initialize a list with zeroes or some other values, that way you can't get out of range error Commented Nov 4, 2021 at 16:49
  • if you are looking at mathematic way without using additional library you can read this. integratedmlai.com/matrixinverse Commented Nov 4, 2021 at 16:53

2 Answers 2

0

You can use zip on the reversed array:

auxiliaryArray = list(zip(*multiarray[::-1]))

or

auxiliaryArray = list(zip(*reversed(multiarray)))

output: [(7, 4, 1), (8, 5, 2), (9, 6, 3)]

If you need lists and not tuples:

auxiliaryArray = list(map(list, zip(*reversed(multiarray))))

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

Sign up to request clarification or add additional context in comments.

4 Comments

Could it be done with append?
Why? Is this an assignment?
Something like that
I take this as a yes… Yes it can be done, but you should have made it explicit from the beginning that it was an assignment (cf. SO guidelines), your question wouldn't have been closed
0

This does it:

list(zip(*multiarray[::-1]))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.