Lists into lists
Mark Day
mday at apple.com
Mon Aug 20 13:58:37 EDT 2001
In article <mailman.998319210.23592.python-list at python.org>, Ignacio
Vazquez-Abrams <ignacio at openservices.net> wrote:
> Inserting y into x just inserts a reference to y into x. You need to insert
> y[:] into x, which then makes a copy of y.
As another poster mentioned, you could use list(y) instead of y[:].
Can anyone offer guidance about which one to choose? Are there subtle
differences in behavior? Is one better than the other when y is a
user-defined class (such as a UserList derivative) vs. a built-in type?
I find the slicing notation a little confusing since slices of xrange()
or Numeric.array don't make copies (at least not by default). Plus, I
could see utility in slicing a generator such that the slice doesn't
make copies either (just yielding the selected items, skipping over the
rest).
By the way, I was slightly surprised that lists don't have a copy()
method like dictionaries. y.copy() would be seem a bit more obvious.
-Mark
More information about the Python-list
mailing list