Mutability, copying lists but not sharing?

cnb circularfunc at yahoo.se
Mon Aug 25 20:46:18 EDT 2008


Is it not possible to have mutability without this? I know I can use
sorted and list(reversed) instead of .sort and .reverse but if I want
to copy a list and then change that list without changing the first
one?
And there isn't a .copy function so I have to "new = [] for element in
list: new.append(element)" ?
(I guess mutability is there for performance? Because I prefer a =
sorted(a) conceptually.)

>>> a = [1,2,3]
>>> b = a
>>> b.append(4)
>>> b
[1, 2, 3, 4]
>>> a
[1, 2, 3, 4]
>>> c = "hello"
>>> d = c
>>> d += " sir!"
>>> c
'hello'
>>> d
'hello sir!'
>>>



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



More information about the Python-list mailing list