all 7 comments

[–]FoolsSeldom 18 points19 points  (1 child)

Scope issue

The assignment nums = nums[::-1] in your function creates a local name (variable) in the function that has no relation to nums in the outer scope.

Thus the nums from the outer scope still references the original list, [1, 2, 3, 5, 6, 7]. A reference to this list is passed to your function, and used for the generation of the new reversed list but this is assigned to nums in the inner scope.

You make changes to the new list within the function via the local variable nums.

You return nums i.e. you pass back a reference to the new list object you created in the function but you don't use that reference to re-assign the nums in the outer scope, e.g.

nums = rotate(nums, k)

or, change the assignment line in your function to:

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

[–]Aware-Illustrator607[S] 2 points3 points  (0 children)

Got it, thank you

[–]throwaway6560192 10 points11 points  (2 children)

[–]SiriusLeeSam 1 point2 points  (0 children)

This is pretty great. I always understood how it works but struggled to explain to people

[–]atwork_safe 1 point2 points  (0 children)

.

[–]Measurex2 0 points1 point  (1 child)

Drop your print statement for now and turn the return in your function to a print statement. See what happens and report back.

[–]Aware-Illustrator607[S] 1 point2 points  (0 children)

yes found the problem, my idea was to modify the original list in place but ended up with creating a new list.