0

I need to create a function that transposes a given matrix (without using numpy or any other additional packages of Python).The matrix can be square or not. I have been able to do it if it is square but not the other case. I have seen with a debugger that the problem is list index out of range but I don't know really how to solve the problem. Thanks for your help.

Edit: I am not allowed to use zip. Is there any other way?

This is my code:


def transpose(M):
    T=[]
    for i in range(0,len(M)):
        row=[0]*len(M[0])
        T.append(row)
    for i in range(len(M)):
        for j in range(len(M[0])):
            T[i][j]=M[j][i]
    return T
print(transpose([[9, 9, 7, 6], [6, 5, 1, 4], [1, 5, 5, 8]]))

UlyssesJA
  • 121
  • 1
  • 4
  • The native `zip` function is ideal for this. – trincot Nov 01 '22 at 18:01
  • And if I am not allowed to use zip? There exist another way? – UlyssesJA Nov 01 '22 at 18:14
  • Seems you are moving the goal posts. What if someone gives you another way... then you might say, "what if I am not allowed to use `append`". And then someone will give a version without that and you might say "what if I am not allowed to use `range`". Not playing that game. Sorry. – trincot Nov 01 '22 at 18:19
  • BTW, the referenced Q&A has an answer that starts with ["Without zip:"](https://stackoverflow.com/a/68772952/5459839) – trincot Nov 01 '22 at 18:27
  • Ok, thanks! And sorry if I bothered you, it was not my intention. – UlyssesJA Nov 01 '22 at 18:32

0 Answers0