-1

let's say I have a 3 nested lists. I would like to create a new nested lists so that the first nested list will contain first values from the prevoius 3 nested lists, the second nested list will contain second values from previous nested lists and so on. The example:

dd = [[5,8,3],[1,4,2],[1,2,3]]

dd = [[5,1,1],[8,4,2],[3,2,3]]
Julian
  • 23
  • 4
  • What have you tried so far? – Johnny John Boy Oct 10 '22 at 09:05
  • Does this answer your question? [Transpose/Unzip Function (inverse of zip)?](https://stackoverflow.com/questions/19339/transpose-unzip-function-inverse-of-zip) and also https://stackoverflow.com/q/12974474/4046632 – buran Oct 10 '22 at 09:08

2 Answers2

1

You can do this with zip,

In [1]: dd = [[5,8,3],[1,4,2],[1,2,3]]

In [2]: list(zip(*dd))
Out[2]: [(5, 1, 1), (8, 4, 2), (3, 2, 3)]

For a list of lists,

In [3]: list(map(list, zip(*dd)))
Out[3]: [[5, 1, 1], [8, 4, 2], [3, 2, 3]]
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
0

You tranpose the matrix like this.

dd = [[5,8,3],[1,4,2],[1,2,3]]

result = []

for i in range(len(dd)):
    temp = []
    for j in range(len(dd)):
        temp.append(dd[j][i])

    result.append(temp)

print(result)
Asif
  • 38
  • 3