Explain the code in python

Hi I need bit of help in understanding the following code in python

def process(data):
       data = [1, 2, 3]
       return data[-2]

measurements = [ 0 for I in range(3) ]

Answer is 0 but I don’t understand how.Please Explain

So you initialized measurement with a bunch of 0s. I think you know that much. The part where it goes wrong is this line here:

data = [1, 2, 3]

I am sure you expected it to reassign the values in the measurements list to that list but what it actually does is reassign the data variable to point to a new list. You need to individually assign the index values or call list methods.

Since you just reassign the data variable, the original measurements list is unaffected by the method call and so the result is 0.

If you change that line to something like this it should work:

data[:] = [1, 2, 3]

That is a slice assignment which is assigning the values in the slice rather than reassigning the variable.