Question about idioms for clearing a list
Steven Bethard
steven.bethard at gmail.com
Tue Feb 7 10:58:04 EST 2006
Ben Sizer wrote:
>> Likewise, the del keyword is fundamental -- if you
>> can't get, set, and del, then you need to go back to collections
>> school.
>
> I have hardly used the del keyword in several years of coding in
> Python. Why should it magically spring to mind in this occasion?
> Similarly I hardly ever find myself using slices, never mind in a
> mutable context.
I just grepped through my codebase, and while I do see a number of dels
with dicts, and a number of dels with instance attributes, I see only
two dels with lists, both from the same module. So I think your point
about infrequent use of del, at least with lists, is pretty valid.
I suspect this is reinforced by the fact that del is usually not the
right way to go when using lists -- repeated dels in the middle of a
list is almost always less efficient than other techniques due to the
underlying array implementation.
That said, I didn't find anywhere in my codebase that I needed to clear
a list (i.e. where ``L = []`` wasn't perfectly fine), while I do find a
small number of dict clears. So I guess I don't really care if clearing
a list is a little less intuitive than clearing a dict.
STeVe
More information about the Python-list
mailing list