copy a list

Wayne Pierce wayne at mishre.com
Mon Mar 10 07:00:21 EST 2003


yuvalfeld at hotmail.com (Yuval) wrote in message news:<6ca96053.0303092333.56aef854 at posting.google.com>...
> Hi, 
> I have to work on a list without changing it. Specifically, in every
> iteration I have to choose an object and remove it in the end of that
> iteration, repeat it until the list is empty. However, I do not want
> to touch the list itself since I need to repeat this process many
> times (start over again once the list is empty). Therefore, in order
> to "play around" with a list A, I create a copy of it: B = A[:].
> My problem is that it slows down my program siginificantly. Is it a
> common problem with that operation? Is there an alternative way of
> soing it?
> 
> Thanks.

How big is the list you are dealing with?  What about a different
approach to the same thing.  Have you looked at using a counter to
keep track of the position in the original list?

Rather than duplicating the list and removing the items when you are
done, just increment the pointer one and leave the original list
alone.  This way you do not have to copy the entire list.

Wayne




More information about the Python-list mailing list