Skip to content

Commit 2880813

Browse files
committed
Create transpose.py
1 parent 024dc59 commit 2880813

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

script_using_ZIP/transpose.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#how to get transpose of a matrix.
2+
# A normal coder may require some loops to get transpose but using ZIP function we can have it as one liner.
3+
# Know the Basic Usage of the Zip Function
4+
#The zip function aggregates items from different iterables, such as lists, tuples or sets, and returns an iterator.It works just like a physical zip.
5+
#In fact, the zip function in Python is much powerful than a physical zipper. It can deal with any number of iterables at once rather than just two.
6+
#Unfortunately, Python doesn’t have an unzip function. However, if we are familiar with the tricks of asterisks, unzipping is a very simple task.
7+
#In the above example, the asterisk did the unpacking operation, which is unpacking all the four tuples from the record list.
8+
9+
#main code
10+
matrix = [[1, 2, 3], [1, 2, 3]]#the inputted matrix
11+
matrix_T = [list(i) for i in zip(*matrix)]#one liner code for taking transpose of matrix
12+
print(matrix_T)#print to validate

0 commit comments

Comments
 (0)