I have two lists of tuples mkp1
and mkp2
which I zip
and want to unpack them later to lists. But after the first part is unpacked, the rest ist missing... Why?
Minimal example:
# list of tuples
mkp1 = [(1, 2), (3, 4), (5, 6)]
mkp2 = [(10, 20), (30, 40), (50, 60)]
# zip this list
pairs = zip(mkp1, mkp2)
# unzip this list
p1 = [kpp[0] for kpp in pairs]
p2 = [kpp[1] for kpp in pairs]
print('p1:', p1)
print('p2:', p2)
Edit: Strangely this works like I expected in Python 2.7 but not in Python 3.4.
Ah, I found the answer: In Python 2, zip returns a list of tuples while in Python 3 it returns an iterator. This causes the second iteration to lead to an empty list.
This works:
# list of tuples
mkp1 = [(1, 2), (3, 4), (5, 6)]
mkp2 = [(10, 20), (30, 40), (50, 60)]
# zip this list
pairs = zip(mkp1, mkp2)
# unzip this list
p1, p2 = [], []
for kpp in pairs:
p1.append(kpp[0])
p2.append(kpp[1])
print('p1:', p1)
print('p2:', p2)