Merge numpy arrays returned from loop
arrays, numpy, python
Solution
You probably mean
allArrays = np.array([])
for x in range(0, 1000):
myArray = myFunction(x)
allArrays = np.concatenate([allArrays, myArray])
A more concise approach (see wims answer) is to use a list comprehension,
allArrays = np.concatenate([myFunction(x) for x in range])
Problem
I have a loop that generates numpy arrays: ``` for x in range(0, 1000): myArray = myFunction(x) ``` The returned array is always one dimensional. I want to combine all the arrays into one array (also one dimensional. I tried the following, but it failed ``` allArrays = [] for x in range(0, 1000): myArray = myFunction(x) allArrays += myArray ``` The error is `ValueError: operands could not be broadcast together with shapes (0) (9095)`. How can I get that to work? For instance these two arrays: ``` [ 234 342 234 5454 34 6] [ 23 2 1 4 55 34] ``` Shall be merge into this array: ``` [ 234 342 234 5454 34 6 23 2 1 4 55 34 ] ```