0

For example, I am given a function that takes in an array to change in place.

def sample(array):
    array[-1] = "e"
    
array = ["a","b","c"]

sample(array[0:2])
print(array)

Ideally, I would like to pass a reference to a subarray (eg.["a","b"]) such that array would also be changed in-place (the output would be ["a","e","c"]).

However, the function outputs

['a', 'b', 'c']

Is there a way to get around this?

  • This may answer your question: [Modifying list slice passed into a function](https://stackoverflow.com/questions/31443811/modifying-list-slice-passed-into-a-function) – sj95126 Sep 22 '21 at 15:16
  • You can also access the method of the list like .append() and so more ! – Da2ny Sep 22 '21 at 15:30

0 Answers0