You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2014-08-02.21:34:29.858>created_at=<Date2014-08-02.20:35:33.904>labels= ['type-bug', 'invalid', 'OS-windows']
title='Rotating items of list to left'updated_at=<Date2014-08-02.21:34:29.856>user='https://bugs.python.org/SaiKrishnaG'
Hi, I am trying to rotate list of 3 to left
for this I am trying to define this function
def rotate_left3(nums)
#argument nums is list
for example
rotate_left3([1, 2, 3])
I am expecting value [2,3,1] in return. I have written the following code
a = nums
a[0]=nums[1]
a[1]=nums[2]
a[2]=nums[0]
return a #this is returning [2,3,2] instead of [2,3,1]
however if I assign
a = [0,0,0] #or any other value other than directly assigning nums
the code works perfectly
This is not a bug. The assignment "a = nums" doesn't create a copy of "nums", it just assigns the name "a" to the same object that "nums" refers to. Since lists are mutable, changes made to "a" are visible through the name "nums". By the time you do "a[2] = nums[0]", "nums[0]" has been reassigned.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: