0

I have to flatten array of lists to 1d numpy array. I use this:

import numpy as np

orig = np.array([[1,2], [3]], dtype=object)

lst = []
for x in orig:
    lst.extend(x)

result = np.array(lst)  # makes array([1, 2, 3])

Is there a standard numpy function or method to do this (similar to np.flatten)?

3
  • 1
    np.hstack(orig.ravel()) Commented May 26, 2022 at 8:50
  • 1
    @Ali_Sh oh indeed, just np.hstack(orig)! Turns out hstack allows arbitrary size internal arrays for 1d dimensional data Commented May 26, 2022 at 9:02
  • hstack treats the argument as a list of arrays - split on the first dimension. Commented May 26, 2022 at 14:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.