If Python slice copy the reference, why can't I use it to modify the original list?
问题 I know that Slicing lists does not generate copies of the objects in the list; it just copies the references to them. But if that's the case, then why doesn't this work? l = [1, 2, 3] # Attempting to modify the element at index 1 l[0:2][-1] = 10 # but the attempt fails. The original list is unchanged l > [1, 2, 3] Shouldn't l[0:2][-1] point to the element at index 1 of the original list? 回答1: You are right that slicing doesn't copy the items in the list. However, it does create a new list