i want to know the difference between list variable itself and listvariable followed by [:]
for example,
# When nums are List[int] and res are List,
# what is the difference between
res.append(nums[:])
# and
res.append(nums)
my question came up while i'm implementing recursive permutation function
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res = []
self.helper(nums, 0, len(nums) - 1, res)
return res
def helper(self, nums, l, r, res):
if l == r:
res.append(nums[:]) # this will append unique combination
res.append(nums) # this will result in a list full of same combinations
else:
for i in range(l, r + 1):
nums[l], nums[i] = nums[i], nums[l]
self.helper(nums, l + 1, r, res)
nums[l], nums[i] = nums[i], nums[l]
Thanks for your help in advance!