Avatar ·

How to rotate a matrix (2D array) by 90 degrees using zip?

📁 Advanced Python Topics, example, numpy

How to rotate a matrix in one line without NumPy and loops?

For example, if the original matrix is:

[[1, 2],[3, 4]]

then the result should be:

[[3, 1],[4, 2]]
Avatar ·

In line 4, you write an integer to the variable "a":

a=random.randint(-5,4)

Then, in line 9, you try to access the variable "a" by index.

a[i]=int(a[i])

But integer variables don't support indexing. To make it work, you need to replace line 4 with this:

a.append(random.randint(-5,4))

In general, your code is not very Pythonic. Where possible, it's recommended to use list comprehensions instead of loops. They are much more concise, and the code with them becomes

Log in to leave an answer

Blogs