It seems so "dirty" emptying a list in this way:
while len(alist) > 0 : alist.pop()
Does a clear way exist to do that?
It seems so "dirty" emptying a list in this way:
while len(alist) > 0 : alist.pop()
Does a clear way exist to do that?
This actually removes the contents from the list, but doesn't replace the old label with a new empty list:
del lst[:]
Here's an example:
lst1 = [1, 2, 3]
lst2 = lst1
del lst1[:]
print(lst2)
For the sake of completeness, the slice assignment has the same effect:
lst[:] = []
It can also be used to shrink a part of the list while replacing a part at the same time (but that is out of the scope of the question).
Note that doing lst = []
does not empty the list, just creates a new object and binds it to the variable lst
, but the old list will still have the same elements, and effect will be apparent if it had other variable bindings.
If you're running Python 3.3 or better, you can use the clear()
method of list
, which is parallel to clear()
of dict
, set
, deque
and other mutable container types:
alist.clear() # removes all items from alist (equivalent to del alist[:])
As per the linked documentation page, the same can also be achieved with alist *= 0
.
To sum up, there are four equivalent ways to clear a list in-place (quite contrary to the Zen of Python!):
alist.clear() # Python 3.3+
del alist[:]
alist[:] = []
alist *= 0
You could try:
alist[:] = []
Which means: Splice in the list []
(0 elements) at the location [:]
(all indexes from start to finish)
The [:] is the slice operator. See this question for more information.
alist
with a different list which happens to be empty. If anyone else had a reference to the original list, that remains as-is (i.e. it contains whatever was in it to begin with)
Feb 13, 2017 at 3:56
alist.clear()
or alist[:] = []
helps checking that alist
truly is a list. Say you got alist
returned from a function foo()
. You thought foo
returned a list but indeed it returned a None
. Using alist = []
cannot catch that mistake.
it turns out that with python 2.5.2, del l[:]
is slightly slower than l[:] = []
by 1.1 usec.
$ python -mtimeit "l=list(range(1000))" "b=l[:];del b[:]"
10000 loops, best of 3: 29.8 usec per loop
$ python -mtimeit "l=list(range(1000))" "b=l[:];b[:] = []"
10000 loops, best of 3: 28.7 usec per loop
$ python -V
Python 2.5.2
.clear()
, del b[:]
and b[:]=[]
all runs the same (b[:]=[]
being slightly slower.
Mar 13, 2017 at 1:13
lst *= 0
has the same effect as
lst[:] = []
It's a little simpler and maybe easier to remember. Other than that there's not much to say
The efficiency seems to be about the same
0
would have the same effect. It is a pretty neat trick though, I'm a bit sad, that it didn't receive much attention..
May 24, 2018 at 9:54
list = []
will reset list
to an empty list.
Note that you generally should not shadow reserved function names, such as list
, which is the constructor for a list object -- you could use lst
or list_
instead, for instance.
list
. If you expected a function to modify a passed in list (for example), this wouldn't do what you want.
Sep 9, 2009 at 16:12
Another simple code you could use (depending on your situation) is:
index=len(list)-1
while index>=0:
del list[index]
index-=1
You have to start index at the length of the list and go backwards versus index at 0, forwards because that would end you up with index equal to the length of the list with it only being cut in half.
Also, be sure that the while line has a "greater than or equal to" sign. Omitting it will leave you with list[0] remaining.