1

I was wondering how you can create a list of values of the same index from another list, when the amount of values is not known beforehand.

for example I have the list

list = [[25,75,94,63],[55,13,0,99],[41,63,93,25]]

I want a second list that would be

[[25,55,41],[75,13,63],[94,0,93],[63,99,25]]

If possible I would like the answer to only use simple python functions and without the use of importing a module. And if possible how to do so using a loop (probably nested?)

Thanks

2 Answers2

3

You're looking for the zip builtin function which does precisely what you'd expect.

THG
  • 312
  • 3
  • 13
0

Try:

a = []
for j in range(len(list1[0])):
    a.append([i[j] for i in list1])

Here is a one-liner:

[[i[j] for i in list1] for j in range(len(list1[0]))]

This gives the output:

[[25, 55, 41], [75, 13, 63], [94, 0, 93], [63, 99, 25]]
Joshua Varghese
  • 5,082
  • 1
  • 13
  • 34