devxlogo

Transpose a Matrix in Python

One of the inbuilt libraries in Python is zip. It can be utilized to transpose a matrix by performing an Unzip followed by zip. Sample code below.  

Python comes with many inbuilt libraries zip is among those. The??zip()??function returns an iterator of tuples based on the??iterable??object. In order to get the transpose of the matrix first, we need to unzip the list using??*??operator then zip it.

inputMatrix = [ [7, 14, 21], [1, 2, 3] ]
zip(*inputMatrix)

Output will be as follows:
[ (7, 1), (14, 2), (21, 3) ] 

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.