Clearing a list

By : Dan

I find it annoying that I can't clear a list. In this example:

a = []

a = []

The second time I initialize a to a blank list, it creates a new instance of a list, which is in a different place in memory, so I can't use it to reference the first, not to mention it's inefficient.

The only way I can see of retaining the same pointer is doing something like the following:

for i in range(len(a)):

This seems pretty long-winded though, is there a better way of solving this?

By : Dan


I'm not sure why you're worried about the fact that you're referencing a new, empty list in memory instead of the same "pointer".

Your other list is going to be collected sooner or later and one of the big perks about working in a high level, garbage-collected language is that you don't normally need to worry about stuff like this.

By : Dana

You are looking for:

del L[:]

I don't think thats possible. If you are saying that the relation has some state than in essence it is an object in it's own right and should be treated (mapped) as such.

By : Jasper

This video can help you solving your question :)
By: admin