Skip to content
Advertisement

Appending to empty array in a for loop (python)

How do I append values in an empty numpy array? I have an array of values that I want to perform a mathematical equation on and from those values I want to append it to a new empty array. What I have is:

JavaScript

However, it isn’t appending and not sure why?

Advertisement

Answer

You’re missing the array name, you’re using values_array instead of values_a. Here’s a code that works using your logic:

JavaScript

Nevertheless, as some comments say, it is not recommended to use numpy.append inside a loop, so you can use scipy.special.factorial instead. It allows calculating the factorial of the whole array element-wise:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement