-1

Suppose we have the function below:

def foo(nums, k):
    add = sum(nums[:k])
    return add

In the function above, we temporarily create a sub-list of length k. We then calculate the sum of this sub-list, storing its value in add.

My question is simple. Since num stores a numeric value (and no actual reference to the sub-list we just created), once this line executes, do we de-allocate the space for the nums sub-list of size k?

LateGameLank
  • 113
  • 5
  • 3
    The sublist will have a zero reference count after the sum operation, so in CPython it will be immediately deallocated. (The language spec does not require this, so other implementations may behave differently.) Note: this doesn't mean that the Python process necessarily releases memory back to the OS. – slothrop Aug 22 '23 at 16:52

0 Answers0