On Sat, Mar 7, 2009 at 2:36 PM, Kapsicum <[email protected]> wrote: > > > On Sun, Mar 8, 2009 at 12:39 AM, sphennings W. <[email protected]>wrote: > >> When I enter the following code into IDLE do both lists have the same >> value? >> How would I manipulate both lists separately? >> >> >>> List1=[1,2,3] >> >>> List2=List1 >> >>> List2.reverse() >> >>> print(List2) >> [3, 2, 1] >> >>> print(List1) >> [3, 2, 1] >> >>> List2.append(0) >> >>> print(List2) >> [3, 2, 1, 0] >> >>> print(List1) >> [3, 2, 1, 0] > > > When you create an object and assign it to a variable, the variable only > refers to the object > and does not represent the object itself. > > > If you want to make a copy of a list or such kinds of sequences , then you > have to use > the slicing operation to make a copy. If you just assign the variable name > to another name, > both of them will refer to the same object. > > List2=List1[ : ] > > > > Kapil Dua > Mobile: +919711311052 > > > > > > _______________________________________________ > Tutor maillist - [email protected] > http://mail.python.org/mailman/listinfo/tutor > > This made me search, and I found another solution: if you use the list function as follows: >>> s = [1,2,3] >>> v = list(s) >>> s.reverse() >>> s [3, 2, 1] >>> v [1, 2, 3] >>>
As a linguist, I would love to know what the difference between these things are: mycopy = original[:] mycopy = copy.deepcopy(original) mycopy = list(original) -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com --------------------------------------------------------
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
