Home > Software design >  Python list reference and just another copy
Python list reference and just another copy

Time:10-10

I wish to bring that last k elements of 'nums' array to the first. Like, Input:

nums = [1,2,3,4,5,6,7], k = 3

Output:

[5,6,7,1,2,3,4]

I have the following code:

class Solution(object):
    def rotate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: None Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        k = k % n
        nums[:] = nums[n-k:]   nums[:n-k]

This works perfectly fine i.e. brings the last k elements to the beginning and nums shows [5,6,7,1,2,3,4]. But once I type the following nums = nums[n-k:] nums[:n-k], it shows the resultant nums array as being the same original [1,2,3,4,5,6,7].

My question is, why is the change in output taking place? On googling and upon reding certain other threads in this forum pertaining to 'List Referencing and copying', I could realize that nums = is something about list referencing but nums[:] is like a mere copy of the list. But having said thiat, why is this change in output taking place? What's happening inside both the commands?

Seems like nums and nums[:] aren't yet clear to me. Please help.

CodePudding user response:

Explanation

nums = nums[n-k:] nums[:n-k]

The assignment nums = nums[n-k:] nums[:n-k] 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. Check follow diagram for better understanding:

1


nums[:] = nums[n-k:] nums[:n-k]

The assignment nums[:] = nums[n-k:] nums[:n-k] 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.

2

In this case you are not changing the variable nums, but the data pointed by it.

Example Code:

def rotate(nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: None Do not return anything, modify nums in-place instead.
    """
    n = len(nums)
    k = k % n
    # in function
    # nums[:] = nums[n-k:]   nums[:n-k]
    nums = nums[n-k:]   nums[:n-k]

# in scope
nums = [1,2,3,4,5,6,7]
k = 3
rotate(nums, k)
print(nums)

  • Related