Em Ter, 2006-04-11 Ã*s 17:56 +0000, John Salerno escreveu:

> Steven Bethard wrote:

>

>

> > lst[:] = []

> > lst = []

>

> What's the difference here?

lst[:] = [] makes the specified slice become []. As we specified ":", it

transforms the entire list into [].

lst = [] assigns the value [] to the variable lst, deleting any previous

one.

This might help:

>>> lst = range(10)

>>> id(lst), lst

(-1210826356, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> lst[:] = []

>>> id(lst), lst

(-1210826356, [])

>>> lst = range(10)

>>> id(lst), lst

(-1210844052, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

>>> lst = []

>>> id(lst), lst

(-1210826420, [])

You see? lst[:] removes all elements from the list that lst refers to,

while lst = [] just creates a new list and discard the only one. The

difference is, for example:

>>> lst = range(3)

>>> x = [lst, lst, lst]

>>> x

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

>>> lst[:] = []

>>> x

[[], [], []]

>>> lst = range(3)

>>> x = [lst, lst, lst]

>>> x

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

>>> lst = []

>>> x

[[0, 1, 2], [0, 1, 2], [0, 1, 2]]

HTH,

--

Felipe.