0
$\begingroup$

From the NumPy documentation:

"NumPy arrays have a fixed size at creation, unlike Python lists (which can grow dynamically). Changing the size of an ndarray will create a new array and delete the original."

To my understanding, this would make the numpy array immutable, but from what I've seen, they're actually mutable.

When I was trying to learn about the concept of mutability/immutability, it was with respect to strings, and I was having a hard time understanding why a string was considered immutable if I could, in fact, quite easily manipulate it with, for example, a simple addition operation with another string. What I took away from that inquiry was that the key distinction was that you're not actually changing the original string. You're creating a new string that's stored in a new variable which lives in a new memory location. Ok, that made sense.

So how is that different from what NumPy is saying it does?

$\endgroup$

1 Answer 1

2
$\begingroup$

Just because the size of the array is fixed doesn't mean it is immutable. In an immutable array, both the size and the contents must be immutable. With a Numpy array, you can change its contents (the values at each position) in place, without creating a new array.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.