# New to Python – Why does this program produce an array of length 1000, instead of 50000, and why are the first 50 values of the array equal to 0?

## Question:

Why does this program produce an array of length 1000, instead of 50000, and why are the first 50 values of the array equal to 0?

``````import numpy as np
arr_growthrates = np.linspace(0, 4, 1000)
arr_initial_x = np.random.random((1, 50))

l = []
for k in arr_growthrates:
for m in arr_initial_x:
iteration = 100

v = []
v.append(m)

for i in range(iteration):
v.append(k * v[i] * (1 - v[i]))

l.append(v[100])

print(len(l))
print(l)
``````

Because this involves a loop within a loop, and the first loop has 1000 iterations, and the second loop has 50, and because I append values to l after the second loop, I expected l to have a length of 50000, but instead it has a length of 1000. Also why are the first 50 values in l equal to 0?

As discussed in the comments, the problem was with the shape of ` np.random.random((1, 50))`. It results in an array of the shape `(1, 50)`, which is equivalent of a list `[[a1, a2, ...]]` from the iteration point of view. Instead, use just:

`````` arr_initial_x = np.random.random(50)
``````

Because `arr_initial_x = np.random.random((1, 50))` returns an array with this shape (1, 50).

So, inside the loop `for m in arr_initial_x:` there is only 1 round done because only one object read (e.g. the whole array of 50 items).

I guess you would have a shape equal to (50,), and you can have this result with this command `arr_initial_x[0].shape`.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.