Last active
January 27, 2019 15:13
-
-
Save CMCDragonkai/0cd2cc8c0aa7fd5eeec3955052dfd344 to your computer and use it in GitHub Desktop.
Transpose a list or tuple in Python #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
l = [ | |
[1,2,3,4], | |
[5,6,7,8], | |
[9,10,11,12] | |
] | |
# the result shape will always produce from shortest row | |
lt = list(map(list, zip(*l))) | |
# [ | |
# [1, 5, 9], | |
# [2, 6, 10], | |
# [3, 7, 11], | |
# [4, 8, 12] | |
# ] | |
t = ( | |
(1,2,3,4), | |
(5,6,7,8), | |
(9,10,11,12) | |
) | |
# the result shape will always produce from shortest row | |
tt = tuple(zip(*l)) | |
# ( | |
# (1, 5, 9), | |
# (2, 6, 10), | |
# (3, 7, 11), | |
# (4, 8, 12) | |
# ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment