Difference between nums[:] = nums[::-1] and nums = nums[::-1]

Har*_*rry 2 python python-3.x

I'm currently learning Python and I encountered an issue with with assignment to a list.

In

def nextPermutation(self, nums: List[int]) -> None:
    ...
Run Code Online (Sandbox Code Playgroud)

I have a line of code that reverses the list as follows:

nums = nums[::-1]
Run Code Online (Sandbox Code Playgroud)

but the test bench marks it as incorrect, whereas

nums[:] = nums[::-1]
Run Code Online (Sandbox Code Playgroud)

is ok.

I suspect it's because I'm creating another nums list object and the original list that's passed in is unchanged, is that right?

JLD*_*iaz 6

If you know about C and pointers, you can picture nums as a pointer to a list.

Consider this case:

def reverse(nums):
    nums = nums[::-1]

my_list = [1, 2, 3]
reverse(my_list)
Run Code Online (Sandbox Code Playgroud)

The assignment nums = nums[::-1] would be equivalent to create a new list in memory (with the elements reversed), and changing the pointer nums to point to that new list. But since the variable nums that you change is local to the function, this change does not affect to the external one passed as parameter, as shown in this picture:

坏

Now consider this case:

def reverse(nums):
   nums[:] = nums[::-1]

my_list = [1, 2, 3]
reverse(my_list)
Run Code Online (Sandbox Code Playgroud)

The assignment nums[:] = nums[::-1] would be equivalent to write a new reversed list at the same address pointed by nums, because the slice allows to replace part of a list with new contents (although in this case the "part" is the whole list).

In this case you are not changing the variable nums, but the data pointed by it. This is shown in the following picture:

对

Note that in fact all variables in python are "pointer" in the sense used in this answer, but only lists and dicts allow to replace "in-place" some contents (they are mutable data types). With any other type (inmutable) you cannot alter the data in memory, so all you can do is to "reassign the pointer" as in the first case, and this is the reason why a function cannot change the data received as parameter, except for the list case.

  • @AndrejKesely哈哈!这不是秘密。我使用了https://www.draw.io,它很棒,免费且在线 (2认同)

Ada*_*Er8 5

your assumption is exactly right.

we can verify this by using the builtin function id to get the address of the object in memory

try to run this:

def func(nums):
    print(id(nums),"before change")
    nums[:] = nums[::-1]
    print(id(nums), "after [:]")
    nums = nums[::-1]
    print(id(nums), "after regular assign")

a = [1,2,3]
print(id(a),"original")
func(a)
Run Code Online (Sandbox Code Playgroud)

Output (numbers may vary):

55313512 original
55313512 before change
55313512 after [:]
55297688 after regular assign
Run Code Online (Sandbox Code Playgroud)

as you can see, the last id returns a different value, while the rest (from within and from outside of the function) return the same one.