-2

Hopefully I can explain this well I have a list that is n in length but for this example lets use this.

List = [[['a','b','c'], [1,2,3], [7,8,9]],[['e','f','g'], [4,5,6], [1,2,3]]]

I want to change up all the values to yield

List = [[['a',1,7], ['b',2,8], ['c',3,9]], [['f',4,1], ['g',5,2], ['c',6,3]]]

so essentially List[0][0], List[1][0], List[2][0] and so on. I have tried a bunch of stuff and I cant quite find the write mix to get this working. any help would be appreciated

I have attempted too many things to count and non of them are particularly valuable so ill leave that out for now.

1 Answers1

2

Use zip!

[[list(x) for x in zip(*sublist)] for sublist in List ]

Or, with unequal lengths:

[map(None, *sublist) for sublist in List] # Python 2
[list(itertools.zip_longest(*sublist)) for sublist in List] # Python 3

If you can be ensured that the sublists are of equal length and tuples would suffice instead of lists, then:

[zip(*sublist) for sublist in List]
Jared Goguen
  • 8,772
  • 2
  • 18
  • 36