[Tutor] 2 problems in a small script
Aditya Lal
aditya.n.lal at gmail.com
Fri Oct 12 09:18:47 CEST 2007
lstB = lstA
The above just points lstB to lstA and *does not* create a copy. If you do
not understand "pointers", think of lstB an alias of lstA. If you want to
create a copy you need to explicitly do it.
lstB = []
for word in lstA :
lstB.append(word)
or shortcut
lstB = lstA[:]
So, even though you created a new variable lstB it was actually modifying
lstA.
HTH
Aditya
On 10/12/07, Dick Moores <rdm at rcblue.com> wrote:
>
> At 11:25 PM 10/11/2007, Aditya Lal wrote:
> >Hi Dick,
> >
> >You are deleting from the SAME list that you are traversing. This
> >results in problems.
>
> Aditya,
>
> Well, if deleting from the same list I'm traversing is the problem,
> why doesn't inserting the line
> lstB = lstA
>
> and deleting from lstB clear it up? The output is exactly the same.
> I'm really puzzled. See <http://www.rcblue.com/Python/buggy_For_Web2.py>
>
> I thank you for showing me another way to get the result I want, but
> first I want to know why my code didn't work.
>
> Dick
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
Aditya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071012/9f48f1cc/attachment.htm
More information about the Tutor
mailing list