34

Please what is the most efficient way of emptying a list?

I have a list called a = [1,2,3]. To delete the content of the list I usually write a = [ ]. I came across a function in python called del. I want to know if there is a difference between del a [:] and what I use.

kylieCatt
  • 10,672
  • 5
  • 43
  • 51
ebenezer popoola
  • 477
  • 7
  • 18

4 Answers4

63

There is a difference, and it has to do with whether that list is referenced from multiple places/names.

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

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

Using del a[:] clears the existing list, which means anywhere it's referenced will become an empty list.

Using a = [] sets a to point to a new empty list, which means that other places the original list is referenced will remain non-empty.

The key to understanding here is to realize that when you assign something to a variable, it just makes that name point to a thing. Things can have multiple names, and changing what a name points to doesn't change the thing itself.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • 1
    This example was clear and simple to understand – ebenezer popoola May 31 '15 at 19:08
  • I'd also point out that `a = []` is faster than `del a[:]` – JosiahDaniels May 31 '15 at 19:45
  • 3
    @JosiahDaniels, that technically only remains true if `a` is not the last reference to the list. If it is, then reassigning it will trigger garbage collection to essentially call `del a[:]` anyway behind the scenes. – mintchkin May 31 '15 at 21:38
  • @Amber @mintchkin Do you have any information on runtime? To me it seems that `del a[:]` has to iterate over every member of `a` (and then alter the list e.g. resize or something?) whereas `a = []` "just" creates a new list and abandones the old one (which then has to be gced; maybe at a "more appropriate time"? e.g. deleting lots of lists and only starting gc one time vs. starting gc each time (increased overhead)). – syntonym Jun 01 '15 at 07:39
  • @syntonym: gc doesn't work like that in CPython. It reference-counts first, and only defers to a more appropriate time when there are reference loops that simple refcounting doesn't handle. So `a = []`, where `a` is the only reference to its referand, does normally result in immediate disposal of the list that has been abandoned. Freeing the list does what you say, iterate over the contents dereffing (and possibly freeing) each element. But if you used a Python implementation with Java-style gc, then I think `del a[:]` wouldn't have to do the work immediately. Unless I've missed something. – Steve Jessop Jun 01 '15 at 09:07
11

This can probably best be shown:

>>> a = [1, 2, 3]
>>> id(a)
45556280
>>> del a[:]
>>> id(a)
45556280
>>> b = [4, 5, 6]
>>> id(b)
45556680
>>> b = []
>>> id(b)
45556320

When you do a[:] you are referring to all elements within the list "assigned" to a. The del statement removes references to objects. So, doing del a[:] is saying "remove all references to objects from within the list assigned to a". The list itself has not changed. We can see this with the id function, which gives us a number representing an object in memory. The id of the list before using del and after remains the same, indicating the same list object is assigned to a.

On the other hand, when we assign a non-empty list to b and then assign a new empty list to b, the id changes. This is because we have actually moved the b reference from the existing [4, 5, 6] list to the new [] list.

Beyond just the identity of the objects you are dealing with, there are other things to be aware of:

>>> a = [1, 2, 3]
>>> b = a
>>> del a[:]
>>> print a
[]
>>> print b
[]

Both b and a refer to the same list. Removing the elements from the a list without changing the list itself mutates the list in place. As b references the same object, we see the same result there. If you did a = [] instead, then a will refer to a new empty list while b continues to reference the [1, 2, 3] list.

paidhima
  • 2,312
  • 16
  • 13
6
>>> list1 = [1,2,3,4,5]
>>> list2 = list1

To get a better understanding, let us see with the help of pictures what happens internally.

>>> list1 = [1,2,3,4,5]

This creates a list object and assigns it to list1. Initialization of list1 in memory

>>> list2 = list1

The list object which list1 was referring to is also assigned to list2.

Now, lets look at the methods to empty an list and what actually happens internally.

METHOD-1: Set to empty list [] :

>>> list1 = []
>>> list2
[1,2,3,4,5]

Set list1 to empty list

This does not delete the elements of the list but deletes the reference to the list. So, list1 now points to an empty list but all other references will have access to that old list1.
This method just creates a new list object and assigns it to list1. Any other references will remain.

METHOD-2: Delete using slice operator[:] :

>>> del list1[:]
>>> list2
[]

Delete all elements of list1 using slice operator

When we use the slice operator to delete all the elements of the list, then all the places where it is referenced, it becomes an empty list. So list2 also becomes an empty list.

Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
0

Well, del uses just a little less space in the computer as the person above me implied. The computer still accepts the variable as the same code, except with a different value. However, when you variable is assigned something else, the computer assigns a completely different code ID to it in order to account for the change in memory required.

rassa45
  • 3,482
  • 1
  • 29
  • 43