Unable to append values in Numpy Array

a=np.arange(10,49)
b=np.array([])
for num in range(len(a)//2,-1):
    b=np.append(a[num])
print(b)

This still gives an empty array as output.
Need Help.

1 Like

There are two Poblems with your code if you want that all the elements of a get added in b

  1. The for loop is never been runned…
    As your array a has length 39 then len(a)//2 is 19 and your range would become…(19, -1) and that is nothing…just go ahead and try to do this…
    for i in range(19, -1): print(i)
    You’ll get nothing…Hope you understand…

  2. The append function that you’ve used is not implemented correctly…
    I think you have some problem in append method of numpy…
    The append method basically takes two arguements (for your purpose only otherwise it takes 3) first arguement is in which array you want to append…so in your case that would be b and second arguement is the values which in your case is a single value ( but there can be more than value appended)…so you would give second arguement as [a[num]]…which would be considered as an array with single element…and then it will return that array which you are storing in b which is correct…

Hope this helps to solve your problem…
Thank you

1 Like

Thanks for your help.

1 Like