Bug?

Erik Max Francis max at alcyone.com
Tue Jul 24 20:49:28 EDT 2001


Ricardo Correia wrote:

> ---------------------------------------------------
> mainlist = [1, 2, 3, 4, 5]
> copy = mainlist
> 
> for item in copy:
>  print item,
>  mainlist.remove(item)
> ---------------------------------------------------
> 
> Shouldn't this produce '1 2 3 4 5'?
> 
> I only get '1 3 5' and unfortunately because of this my program
> doesn't work.

The problem is that the variable named copy isn't a copy, it's a
reference.  Try instead:

	copy = mainlist[:] # now copy is truly a copy

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ What is now prov'd was once only imagin'd.
\__/ William Blake
    Alcyone Systems' Daily Planet / http://www.alcyone.com/planet.html
 A new, virtual planet, every day.



More information about the Python-list mailing list