0

I'm trying to make a nested list using data from other lists. I want to make a list where the n items from other lists are at n position in the outer list.

red = [1, 2, 3]
green = [4, 5, 6]
blue = [7, 8, 9]

What I tried doing:

index= []
[index.append(n)for n in (red, green, blue)]   
print(index)

What I got:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

What I want:

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
dawed1999
  • 335
  • 1
  • 3
  • 11

1 Answers1

3

What you really need is this:

list(map(list, zip(red, green, blue)))

which is equivalent to:

[list(l) for l in zip(red, green, blue)]

which returns:

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Rocky Li
  • 5,641
  • 2
  • 17
  • 33