241

Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python?

old_list = []
old_list = list()

The reason I ask is that I just saw this in some running code:

del old_list[ 0:len(old_list) ]
martineau
  • 119,623
  • 25
  • 170
  • 301
johannix
  • 29,188
  • 15
  • 39
  • 42
  • 1
    Why are you doing this? Garbage Collection works. Why not simply ignore the old list value and let Python clean it up automatically? – S.Lott May 12 '09 at 02:15
  • 44
    @S.Lott: The poster didn't write the code, and is asking why the original writer may have done so. – John Fouhy May 12 '09 at 03:20
  • 21
    Not related to your question, but you shouldn't use len when specifying a slice. `a[:x]` means beginning to x and `a[x:]` means x to end. `a[ 0:len(a) ]` can be written as `a[:]`. You can also use negatives to count from the end (`a[-1]` is the last element). – idbrii Feb 23 '11 at 20:53
  • 1
    You could also do: while l: l.pop() – RoadieRich May 13 '09 at 02:10
  • 13
    LOL. I'm sure that's efficient. – FogleBird May 13 '09 at 02:17
  • 1
    Nobody said anything asbout it being efficient... – RoadieRich May 13 '09 at 03:11

8 Answers8

385

Clearing a list in place will affect all other references of the same list.

For example, this method doesn't affect other references:

>>> a = [1, 2, 3]
>>> b = a
>>> a = []
>>> print(a)
[]
>>> print(b)
[1, 2, 3]

But this one does:

>>> a = [1, 2, 3]
>>> b = a
>>> del a[:]      # equivalent to   del a[0:len(a)]
>>> print(a)
[]
>>> print(b)
[]
>>> a is b
True

You could also do:

>>> a[:] = []
Rufflewind
  • 8,545
  • 2
  • 35
  • 55
Koba
  • 5,216
  • 2
  • 22
  • 13
75

Doing alist = [] does not clear the list, just creates an empty list and binds it to the variable alist. The old list will still exist if it had other variable bindings.

To actually clear a list in-place, you can use any of these ways:

  1. alist.clear() # Python 3.3+, most obvious
  2. del alist[:]
  3. alist[:] = []
  4. alist *= 0 # fastest

See the Mutable Sequence Types documentation page for more details.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • 5
    I have tested and alist *= 0 is the fastest, next fastest is alist.clear() and other two methods are equally fast. Could you explain why alist*= 0 is the fastest? – Jenny Jul 24 '19 at 13:09
  • 6
    @Jenny: It simply uses fewer (or less expensive) [opcodes](https://docs.python.org/3/library/dis.html) under the hood. For example, the operator version doesn't need to look up any method names or build slices. – Eugene Yarmash Jul 24 '19 at 13:28
  • 5
    "There should be one-- and preferably only one --obvious way to do it." Python is shining here :) – Nik O'Lai Jun 04 '20 at 15:08
  • 1
    Nik, you got my upvote for citing the Zen, but I do not see too many ways to "do it" here… First of all, 4 is certainly not "obvious". And 3 is not doing exactly the same as the other options – it first creates an empty list, from which it then replaces the contents of `alist`. The RHS could also be a populated list, so this option is more powerful, but also slightly more expensive. Finally, I prefer the readability of 1 over 2 and agree with the "most obvious" comment, because "Readability counts", and "Beautiful is better than ugly" anyhow. ;-) – hans_meine May 24 '22 at 17:06
38

There is a very simple way to clear a python list. Use del list_name[:].

For example:

>>> a = [1, 2, 3]
>>> b = a
>>> del a[:]
>>> print a, b
[] []
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
NixMan
  • 543
  • 4
  • 9
13

It appears to me that del will give you the memory back, while assigning a new list will make the old one be deleted only when the gc runs.matter.

This may be useful for large lists, but for small list it should be negligible.

Edit: As Algorias, it doesn't matter.

Note that

del old_list[ 0:len(old_list) ]

is equivalent to

del old_list[:]
besen
  • 32,084
  • 1
  • 21
  • 12
9
del list[:] 

Will delete the values of that list variable

del list

Will delete the variable itself from memory

rassa45
  • 3,482
  • 1
  • 29
  • 43
5

There are two cases in which you might want to clear a list:

  1. You want to use the name old_list further in your code;
  2. You want the old list to be garbage collected as soon as possible to free some memory;

In case 1 you just go on with the assigment:

    old_list = []    # or whatever you want it to be equal to

In case 2 the del statement would reduce the reference count to the list object the name old list points at. If the list object is only pointed by the name old_list at, the reference count would be 0, and the object would be freed for garbage collection.

    del old_list
Alex
  • 43,191
  • 44
  • 96
  • 127
4

If you're clearing the list, you, obviously, don't need the list anymore. If so, you can just delete the entire list by simple del method.

a = [1, 3, 5, 6]
del a # This will entirely delete a(the list).

But in case, you need it again, you can reinitialize it. Or just simply clear its elements by

del a[:]
lavee_singh
  • 1,379
  • 1
  • 13
  • 21
-3

another solution that works fine is to create empty list as a reference empty list.

empt_list = []

for example you have a list as a_list = [1,2,3]. To clear it just make the following:

a_list = list(empt_list)

this will make a_list an empty list just like the empt_list.

Johannes Jander
  • 4,974
  • 2
  • 31
  • 46