job - 10 months ago 60
Python Question

# Why is there no list.clear() method in python?

Inspired by this question.

Why is there no list.clear() method in python? I've found several questions here that say the correct way to do it is one of the following, but no one has said why there isn't just a method for it.

del lst[:]
lst[:] = []


While it may go against the "zen of python" to have more than one way of doing something, it certainly seems more obvious to me to have a "list.clear()" method. It would also fall in line with dicts and sets, both of which have .clear().

I came across a few posts to the python-dev and python-ideas concerning this and didn't come to a definitive answer (see here (2006) and here (2009)). Has Guido weighed in on it? Is it just a point of contention that hasn't been resolved yet over the last 4-5 years?

Update: list.clear() was added to python in 3.3 - see here

I guess the .clear() method just never did cross the implicit -100 points rule to become something worth adding to the core language. Although given that the methodname is already used for an equivalent purpose and the alternative can be hard to find, it probably isn't all that far from it.