Java has Arrays.fill(A,1). For a pre-existing List A, is there a shortcut for filling the list with 1? I am writing a function that takes an array and changes the array in some ways. Since arrays are pointers, my function will not return an array. The caller will see the changes after my function returns. The first step in my function is to fill the array with 1s. Doing
def my_work(A):
A =[1]*len(A)
# more work on A
does not seem to change A when my_work is done.
So is my only option
for i in range(len(A)):
A[i]=1
or is there a shortcut? Mine looks like a workaround.
If you really want to change
Ain place, theA[:]syntax should work:And here is the relevant section of the tutorial (“assignment to slices”).