Skip to content

Instantly share code, notes, and snippets.

@volf52
Last active September 5, 2017 11:31
Show Gist options
  • Save volf52/f634fbdbfbf211e45c2515ae340d95c0 to your computer and use it in GitHub Desktop.
Save volf52/f634fbdbfbf211e45c2515ae340d95c0 to your computer and use it in GitHub Desktop.
Transpose a matrix (2D array) in Python
def transpose(matrix):
return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
# this one is a bit slower
def t2(matrix):
return map(list, zip(*matrix))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment