List behaviour

Heiko Wundram me+python at modelnine.org
Wed May 17 17:19:25 CEST 2006


Am Mittwoch 17 Mai 2006 17:06 schrieb barberomarcelo at gmail.com:
> Maybe I'm missing something but the latter is not the behaviour I'm
>
> expecting:
> >>> a = [[1,2,3,4], [5,6,7,8]]
> >>> b = a[:]
> >>> b
>
> [[1, 2, 3, 4], [5, 6, 7, 8]]
>
> >>> a == b
>
> True
>
> >>> a is b
>
> False
>

Try an:

>>> a[0] is b[0]

and

>>> a[1] is b[1]

here, and you'll see, that [:] only creates a shallow copy. Thus, the lists in 
the lists aren't copied, they are shared by the distinct lists a and b.

Hope this clears it up.

--- Heiko.



More information about the Python-list mailing list