Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

In

def nextPermutation(self, nums: List[int]) -> None:
    ...

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

nums = nums[::-1]

but the test bench marks it as incorrect, whereas

nums[:] = nums[::-1]

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?

like image 339
Harry Avatar asked Sep 20 '25 05:09

Harry


2 Answers

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)

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:

bad

Now consider this case:

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

my_list = [1, 2, 3]
reverse(my_list)

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:

right

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.

like image 93
JLDiaz Avatar answered Sep 21 '25 21:09

JLDiaz


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)

Output (numbers may vary):

55313512 original
55313512 before change
55313512 after [:]
55297688 after regular assign

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.

like image 36
Adam.Er8 Avatar answered Sep 21 '25 21:09

Adam.Er8