How can I zip a list of tuples in Python?
# From
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
# To
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
How can I zip a list of tuples in Python?
# From
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
# To
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Just use zip and list comprehension.
>>> tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
>>> [list(i) for i in zip(*tuples)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Or,
>>> tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
>>> [[*i] for i in zip(*tuples)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
If you are okay with using numpy then
import numpy as np
l=[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
[tuple(i) for i in (np.array(l).transpose())]