Suppose I have numpy arrays or lists a
and b
with the same lengths, e.g. a = numpy.array([1,2,3]), b = numpy.array([4,5,6])
and I want to create a numpy.array or list of pairs of the form numpy.array([a[i],b[i]])
. The following code in principle works:
P = [numpy.array([a[i],b[i]]) for i in range(len(a))]
However, I suspect that there is a more elegant way to do it. Moreover, the above code does not work in case a
and b
are scalars, rather than arrays. Ideally, I would like the code to be able to handle both cases. Any suggestions?