0

I have a function foo that returns two values. Currently, I am iterating through a list as follows:

A,B = [],[]
for i in arr:
    a,b = foo(i)
    A.append(a)
    B.append(b)

Is there a more pythonic way to accomplish this? I thought the following was sort of ugly, but maybe it's the best way?

A,B = np.array([foo(i) for i in arr]).T

1 Answers1

-1

This is a transpose, for which a classic Python pattern exists:

a, b = zip(*(foo(i) for i in arr))
orlp
  • 112,504
  • 36
  • 218
  • 315