tags:

views:

137

answers:

3

This is the code:

L=[1,2]

L is L[:]

False

Why is this False?

+14  A: 

L[:] (slice notation) means: Make a copy of the entire list, element by element.

So you have two lists that have identical content, but are separate entities. Since is evaluates object identity, it returns False.

L == L[:] returns True.

Tim Pietzcker
+5  A: 

When in doubt ask for id ;)

>>> li = [1,2,4]
>>> id(li)
18686240
>>> id(li[:])
18644144
>>> 
TheMachineCharmer
+2  A: 

The getslice method of list, which is called when you to L[], returns a list; so, when you call it with the ':' argument, it doesn't behave differently, it returns a new list with the same elements as the original.

>>> id(L)
>>> id(L[:])
>>> L[:] == L 
True
>>> L[:] is L
False
dalloliogm