Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between del a[:] and a = [] when I want to empty a list called a in python? [duplicate]

Tags:

python

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.

like image 356
ebenezer popoola Avatar asked May 31 '15 18:05

ebenezer popoola


People also ask

How do you empty a list in Python?

Using del() The del() function you can selectively remove items at a given index or you can also remove all the elements, making the list empty.

Is [] an empty list?

You can create an empty list using an empty pair of square brackets [] or the type constructor list() , a built-in function that creates an empty list when no arguments are passed. Square brackets [] are commonly used in Python to create empty lists because it is faster and more concise.

What is the difference between del () and remove () methods of list?

The remove() method doesn't return any value. pop() returns deleted value. The del keyword can delete the single value from a list or delete the whole list at a time. At a time it deletes only one value from the list.


2 Answers

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.

like image 189
Amber Avatar answered Sep 21 '22 19:09

Amber


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.

like image 24
paidhima Avatar answered Sep 21 '22 19:09

paidhima