Clearing Python lists

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) ]

Python Curses without clearing screen

I would like to use Curses under Python without clearing the screen. The reason is that I would like my app to pop up a simple small menu over the existing screen and soon exit. It is acceptable, thou

Updating and clearing dictionaries in Python

so I have a question regarding dictionaries in Python. I’d like to create a dictionary where the user will be prompted with 2 options; either updating the dictionary or clearing the dictionary. Let me

Clearing Python shell

How to clear Python shell ? I am writing a module in python, I want to save it in a file. what is the best way to do it?

is clearing lists before returning good?

I’m working with C# and in some methods I use several lists for database results. These can be quite large (10 lists of about 1,000 objects each). For the moment, I just return from these methods and

Merge two lists of lists – Python

This is a great primer but doesn’t answer what I need: http://stackoverflow.com/questions/464342/combining-two-sorted-lists-in-python I have two Python lists, each is a list of datetime,value pairs: l

Curses window in Python without clearing the terminal

Is there a way to initialize curses in Python without clearing the existing text in the terminal? What I have in mind is, that when I will execute my application, it will either push the existing te

Comparing Python lists

I have several long lists in python and have compare them and find the lists that are equal to each other except the last elements in the them. Which is the fastest way?

Manipulating Lists within lists in python

I’m having trouble wrapping my head around dealing with lists within lists in python (I am rather new at programming). Now how would I access each nested list position and then manipulate the position

Python Values in Lists

I am using Python 3.0 to write a program. In this program I deal a lot with lists which I haven’t used very much in Python. I am trying to write several if statements about these lists, and I would l

Who is responsible for clearing up memory from image lists?

If I have a CImageList object (a simple wrapper around a HIMAGELIST), and I call: m_pImageList->Replace(…); http://msdn.microsoft.com/en-us/library/k10cwcdb.aspx Who is responsible for clearing

Answers

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[:] = []

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[:]

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

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

For example:

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

del list[:] 

Will delete the values of that list variable

del list

Will delete the variable itself from memory

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[:]

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.

To clear a list in-place in Python 3.3+ you can also use list.clear(), which is consistent with the interfaces of other mutable containers (such as dict, set or deque).

old_list.clear()  # removes all items from old_list (same as del old_list[:])