List of lists in the list of tuples reordered

Like pythonic-ly, I can do this:

[[x1,y1], [x2,y2]] 

IN:

 [(x1,x2),(y1,y2)] 
+6
source share
2 answers

Use the zip and unpack operator.

 >>> l = [['x1','y1'], ['x2','y2']] >>> zip(*l) [('x1', 'x2'), ('y1', 'y2')] 
+10
source

Handled more cases in the test case.

If there are items in the list that have different lengths.

 In [19]: a Out[19]: [[1, 2], [3, 4], [5, 6], [7, 8, 9]] In [20]: import itertools In [21]: b = itertools.izip_longest(*a) In [22]: list(b) Out[22]: [(1, 3, 5, 7), (2, 4, 6, 8), (None, None, None, 9)] 
+2
source

All Articles