Search code examples
python-3.xlist-manipulation

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


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.


Solution

  • 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)]