Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active January 27, 2019 15:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/0cd2cc8c0aa7fd5eeec3955052dfd344 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/0cd2cc8c0aa7fd5eeec3955052dfd344 to your computer and use it in GitHub Desktop.
Transpose a list or tuple in Python #python
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