-1

new to python so i am writing this code that takes lists and returns a transpose version of it for example

mat = [[1,2],[3,4],[5,6]]
mat_T = mat_transpose(mat)
print(mat)
# [[1, 2], [3, 4], [5, 6]]
print(mat_T)
# [[1, 3, 5], [2, 4, 6]]  

this is an example of a correct output now how do i access the ints in a way that i can add them to a new list like the ints 1 3 5 are all in different lists but i need the in the same list and so are the ints 2 4 6 and so on if there were more ints in more lists

buran
  • 13,682
  • 10
  • 36
  • 61
sds
  • 1
  • 3

1 Answers1

1

You don't need to access the individual values, use zip to iterate over the transposed values and map+list to convert each transposed subarray into list:

mat = [[1,2],[3,4],[5,6]]
mat_T = list(map(list, zip(*mat)))
mat_T

output:

>>> mat_T
[[1, 3, 5], [2, 4, 6]]
mozway
  • 194,879
  • 13
  • 39
  • 75