Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96264

Zipping nested lists in Python

Say I have the following two lists/numpy arrays:

List1 = [[1,2,3,4], [10,11,12], ...]
List2 = [[-1,-2-3,-4], [-10,-11,-12], ...]

I would like to obtain a list that holds the zipping of the nested lists above:

Result = [[(1,-1), (2,-2), (3,-3), (4,-4)], [(10,-10), (11, -11), (12,-12)], ...]

Is there a way to do this with a one-liner (and in a Pythonic way)?

Upvotes: 2

Views: 164

Answers (1)

arshajii
arshajii

Reputation: 129507

l1 = [[1,2,3,4], [10,11,12]]
l2 = [[-1,-2,-3,-4], [-10,-11,-12]]

print [zip(a,b) for a,b in zip(l1,l2)]
[[(1, -1), (2, -2), (3, -3), (4, -4)], [(10, -10), (11, -11), (12, -12)]]

Upvotes: 7

Related Questions