Mutability, copying lists but not sharing?

Alan G Isaac alan.isaac at gmail.com
Mon Aug 25 20:56:39 EDT 2008


cnb wrote:
> And there isn't a .copy function so I have to "new = [] 
> for element in list: new.append(element)"?

You can do
     new = list(old)
which I like for being explicit, or just
     new = old[:]

> and what is the difference between extend and + on lists? 

     >>> a = range(3)
     >>> b = a + range(3)
     >>> b
     [0, 1, 2, 0, 1, 2]
     >>> a
     [0, 1, 2]
     >>> a.extend(range(3))
     >>> a
     [0, 1, 2, 0, 1, 2]

hth,
Alan Isaac



More information about the Python-list mailing list