2

I'm trying to reshape tuple array:

x = [(1,2), (5,4), (3,6)]

into two arrays: [1, 5, 3] and [2, 4, 6] in one line

This is working:

a = [x[0] for x in X]
b = [x[1] for x in X]

But this is not working:

(a, b) = [(x[0], x[1]) for x in X]

How can I do it in one line?

siadajpan
  • 23
  • 2
  • 1
    https://stackoverflow.com/questions/19339/transpose-unzip-function-inverse-of-zip Not sure if a direct duplicate... – Eugene Sh. Aug 10 '17 at 18:51
  • That's definitely the answer. I think it's fine not to mark this as a duplicate, although an argument could certainly be made. – jhpratt Aug 10 '17 at 18:53

1 Answers1

0

You could do it with zip:

a, b = zip(*x)
Jonas Adler
  • 10,365
  • 5
  • 46
  • 73