PDA

View Full Version : python references/values question



flyingsliverfin
April 11th, 2011, 04:15 AM
I was wondering how to stop python from making a reference to value of a list. For example,

a = list1[0]
b = list1[4]

so then if I do

list1[0] = b
list1[4] = a

this doesn't switch the values, does it? to do that i reverted to something like:
list[0], list[4] = list[4], list[0]

I know that to make a new copy of a list] i do x = list[1][:]. Is there a way to do this for integers or values of a list?

DaithiF
April 11th, 2011, 09:15 AM
this doesn't switch the values, does it?
yes it does:


>>> list1= [ 10, 20, 30, 40, 50 ]
>>> a = list1[0]
>>> b = list1[4]
>>> list1[0] = b
>>> list1[4] = a
>>> list1
[50, 20, 30, 40, 10]