0

I am trying to find a nice way of appending a tuple into a nested list using a functional style. The code I am trying to replace is:

a = [[], []]

point = [(10, 12), (11, 13), (14, 15)]

for item in point:
    a[0].append(item[0])
    a[1].append(item[1])
>>> [[10, 11, 14], [12, 13, 15]]

So far I have come up with this, but it seems i've overcomplicated it and was wondering if there was a nicer way of doing it:

from functools import partial

map(partial(lambda a, b, c: (a.append(c[0]), b.append(c[1])), a[0], a[1]), point)
print a
>>> [[10, 11, 14], [12, 13, 15]]
kezzos
  • 2,783
  • 3
  • 18
  • 34

1 Answers1

7

Why use all that when what you're looking for is really zip()?

>>> point = [(10, 12), (11, 13), (14, 15)]
>>> list(zip(*point))
[(10, 11, 14), (12, 13, 15)]
TigerhawkT3
  • 46,954
  • 6
  • 53
  • 87