Simple del[] list question

Fredrik Lundh effbot at telia.com
Wed Apr 26 13:42:41 EDT 2000


Matthew Hirsch <meh9 at cornell.edu> wrote:
> What is going on here?
>
> This leaves a intact.
>
> >>> a=[[1,2,3],[4,5,6]]
> >>> b=a[:]
> >>> del b[0]
> >>> b
> [[4, 5, 6]]
> >>> a
> [[1, 2, 3], [4, 5, 6]]
>
>
> This also changes a.
>
> >>> a=[[1,2,3],[4,5,6]]
> >>> b=a[:]
> >>> del b[0][0]
> >>> b
> [[2, 3], [4, 5, 6]]
> >>> a
> [[2, 3], [4, 5, 6]]
>
> Why did a change?  Why suddenly when you go to two dimensions does the
> behavior change?

[:] makes a *shallow* copy.  it doesn't copy the sublists.

</F>





More information about the Python-list mailing list