Le Monday 30 June 2008 16:29:11 python_newbie, vous avez écrit : > On 25 Haziran, 17:44, MRAB <[EMAIL PROTECTED]> wrote: > > On Jun 25, 11:37 am, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote: > > > On 2008-06-25, python_newbie <[EMAIL PROTECTED]> wrote: > > > > On 24 Haziran, 04:33, Terry Reedy <[EMAIL PROTECTED]> wrote: > > > > Thanks for all answers. At the end i ve only one point. If a decide > > > > to copy list to iterate when will i have to do this ? Before the > > > > iteration ? And then iterate through one list and change value of the > > > > other ? > > > > > > Before starting the iteration would be a good point.... > > > > > > I usually do in such cases: > > > > > > for x in mylist[:]: > > > ... > > > > > > making a copy just before the for loop starts. > > > > > > Lately, I have started avoiding in-place modification of lists. > > > Instead, I construct a new list from scratch inside the for-loop, and > > > replace the old list with the newly constructed list afterwards like: > > > > > > new_list = [] > > > for x in mylist: > > > .... > > > new_list.append(x) > > > > > > mylist = new_list > > > > > > by appending a different value than the original or by not appending, > > > you can influence the contents of the new list. > > > > > > I find this solution nicer than in-place modification of an existing > > > list. > > > > > > Sincerely, > > > Albert > > > > And if you were originally doing in-place modification because there > > were also other references to the list then you could just do: > > > > mylist[:] = new_list > > Thanks again. I see that you use two different syntax for lists > "mylist = new_list" and "mylist[:] = new_list" these are same i > think ?
Not at all, try to figure out what happen with the following : >>>[3]: l=[] >>>[4]: g=[] >>>[5]: l == g ...[5]: True >>>[6]: l is g ...[6]: False >>>[7]: l = [4] >>>[8]: l is g ...[8]: False >>>[9]: l == g ...[9]: False >>>[10]: l = g >>>[11]: l is g ...[11]: True >>>[12]: l[:] = [4] >>>[13]: l == g ...[13]: True >>>[14]: l is g ...[14]: True >>>[15]: g ...[15]: [4] -- _____________ Maric Michaud -- http://mail.python.org/mailman/listinfo/python-list