2

I am trying to define a function that transposes a matrix. This is my code:

def Transpose (A):
    B = list(zip(*A))
    return B

Now when I call the function somewhere in the program like such:

Matrix = [[1,2,3],[4,5,6],[7,8,9]]
Transpose(Matrix)
print(Matrix)

The matrix comes out unchanged. What am I doing wrong?

1 Answer 1

7

Your function returns a new value that does not affect your matrix (zip does not change it's parameters). You are not doing anything wrong, that is the correct way of doing things. Just change it to:

print(Transpose(Matrix))

or

Matrix = Transpose(Matrix)

Note: You really should be using lower-case names for your functions and variables.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.