Python reorder lists inside lists -


this question has answer here:

hopefully can explain have list n in length 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 change values yield

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

so list[0][0], list[1][0], list[2][0] , on. have tried bunch of stuff , cant quite find write mix working. appreciated

i have attempted many things count , non of them particularly valuable ill leave out now.

use zip!

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

or, unequal lengths:

[map(none, *sublist) sublist in list] # python 2 [list(itertools.zip_longest(*sublist)) sublist in list] # python 3 

if can ensured sublists of equal length , tuples suffice instead of lists, then:

[zip(*sublist) sublist in list] 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -