Newbie-list-problem: how to supress constraining 2 lists - a=b ?

Alex Martelli aleax at aleax.it
Fri Nov 8 06:49:54 EST 2002


HugOnline wrote:

> Hello,
> 
> I'm a Python - newbie and I can not solve the following problem:
> 
>>>>a=[8,7,6,5,4]   # set this list to a
>>>>b=a             # I' want to have two same lists: a and b
>>>>b.sort()        # b should be sorted
>>>>print b         # it's ok
> [4,5,6,7,8]
>>>>print a
> [4,5,6,7,8]        # but a is sorted, too !!!
> 
> How can I suppress the constraining of a and b? I want to have in the
> result two list - a unsorted and b sorted. Is there a switch to supress
> it?

When you want a copy, you ask for a copy.  There are MANY
ways to ask for a copy of a string:

b = a[:]

b = list(a)

b = [item for item in a]

import copy
b = copy.copy(a)


etc, etc.


Alex




More information about the Python-list mailing list