Computer >> Computer tutorials >  >> Programming >> Python

How to Transpose a matrix in Single line in Python?


The NumPy package contains a very handy function transpose() to convert ndarray object into a transposed array

>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])