60

I'm trying to turn a list of 2d numpy arrays into a 2d numpy array. For example,

dat_list = []
for i in range(10):
    dat_list.append(np.zeros([5, 10]))

What I would like to get out of this list is an array that is (50, 10). However, when I try the following, I get a (10,5,10) array.

output = np.array(dat_list)

Thoughts?

3 Answers 3

95

you want to stack them:

np.vstack(dat_list)
Sign up to request clarification or add additional context in comments.

1 Comment

For list of arrays with different shapes, one should use: np.concatenate(list, axis=None)
3

Above accepted answer is correct for 2D arrays as you requested. For 3D input arrays though, vstack() will give you a surprising outcome. For those, use stack(<list of 3D arrays>, 0).

Comments

1

See https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html for details. You can use append, but will want to specify the axis on which to append.

dat_list.append(np.zeros([5, 10]),axis=0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.