Python List Issue

Ron_Adam radam2 at tampabay.rr.com
Sun Mar 27 14:59:11 EST 2005


On Sun, 27 Mar 2005 09:01:20 GMT, "Nick L" <Fearnot003 at mchsi.com>
wrote:

>I've hit a brick wall on something that I'm guessing is pretty simple but
>it's driving me nuts. 

Yes, I've ran across that too a few times.

>    How on earth can I make a complete seperate copy of a list with out it
>being a attached to the original in any way shape or form so that I can
>modifiy if at will and not worry about the original? 

This routine copies a list of lists.


# Makes a copy of a list of lists
# Containing simple data.
def copylistlist(alist):
    if type(alist) is list:
        copy = []
        for i in alist:
            if type(i) is list:
                i = copylistlist(i)
            copy.append(i)
    return copy

bob = [[[0, 0]]]
final = copylistlist(bob)

print 'bob:'bob
print 'Final:'final


This still doesn't create new items within the new list.  but with
literal data consisting of letters and numbers, it will work.

If you are working with a data tree, you may be able to modify this to
do what you want. Just add a test in the inner loop for the data you
want to modify.


>Any ideas, suggestions, comments are greatly appreciated
>thanks
>
>Nick

Hope that helps.

Ron_Adam





More information about the Python-list mailing list