Is there a way of unzipping a list of nested redundant list?

33 Views Asked by At

I have this list:

input = [[[1,2]], [[3,4]], [[5,6]]]

Wanted output:

output = [[1,3,5],[2,4,6]]

I have tried this:

x, y = map(list,zip(*input))

to later realize that this method wont work because of the redundant square brackets, is there a way to solve this without iteration.

2

There are 2 best solutions below

0
On BEST ANSWER

You can try this:

>>> from operator import itemgetter
>>> input = [[[1,2]], [[3,4]], [[5,6]]]
>>> list(zip(*map(itemgetter(0), input)))
[(1, 3, 5), (2, 4, 6)]
0
On
In [117]: input = [[[1,2]], [[3,4]], [[5,6]]]

In [118]: list(zip(*[i[0] for i in input]))
Out[118]: [(1, 3, 5), (2, 4, 6)]

In [119]: list(map(list, zip(*[i[0] for i in input])))
Out[119]: [[1, 3, 5], [2, 4, 6]]