As a beginner to programming I was trying to do this and that with Python. I wanted to have a simple function that takes a list as its arguments, and returns another list which is simply the original list rotated once (so rotate([1, 2, 3])
would return [2, 3, 1]
), while keeping the original list unaltered.
I know that this one
def rotate(list):
list.append(list[0])
list.remove(list[0])
would change the list in place (and return None).
But this one
def rotate_2(list):
temp = list
temp.append(temp[0])
temp.remove(temp[0])
return temp
would also change the original list in place (while returning the desired list).
And the third one
def rotate_3(list):
temp = [x for x in list]
temp.append(temp[0])
temp.remove(temp[0])
return temp
gives the desired result, that is returning a new list while keeping the original one untouched.
I can't understand the behaviour of rotate_2
. Why would list
be changed when the function is just doing something on temp
? It gives me a feeling as if list
and temp
is 'linked' by temp = list
. Also why is rotate_3
ok? Sorry if my English is strange, it's not my first language (unlike Python).