tags:

views:

2897

answers:

3

It seems so "dirty" emptying a list in this way:

while len(alist) > 0 : alist.pop()

Does a clear way exist to do that?

+4  A: 
list = []

will reset list to an empty list.

Note that you generally should not shadow reserved function names, such as list, which is the constructor for a list object -- you could use lst or list_ instead, for instance.

Mark Rushakoff
Same here - oh well. Cheap rep is sooo hard to come by!
Harper Shelby
No: this won't modify the list, this just assigns an empty list to the variable `list`. If you expected a function to modify a passed in list (for example), this wouldn't do what you want.
Adam Batkin
The question was ambiguous.
Mark Rushakoff
Not really. The question is "How to empty a list" not "How to assign over a variable that contains a list"
Adam Batkin
the question wasn't ambiguous, the op's snippet was popping elements out of the list (that's it, modifying it in place)...
fortran
+1 to Adam Batkin really good comment in my opinion. I'll accept fortran answer even thought using the while/pop way I can eventually debug what's going away!!!
DrFalk3n
Also, "list" should not be used as a variable name, because is shadows the "list" type. Many Python developers use "L" as a list variable name, but I prefer "lst".
steveha
+31  A: 

This actually removes the contents from the list, not replaces the old label with a new empty list

del l[:]

example:

l1 = [1, 2, 3]
l2 = l1
del l1[:]
print(l2)

For the sake of completeness, slice assignment achieves the same effect:

l[:] = []

and can be used to shrink a part of the list while replacing a part at the same time (but is out of scope of the question).

Note that doing l = [] does not empty the list, just creates a new object and binds it to the variable l, but the old list will still have the same elements, and effect will be apparent if it had other variable bindings.

fortran
Two further questions: What is "del" exactly? ( I deduce it del-etes things but I don't really know what is it ) and 2nd: How do you read ( out loud ) [:]
OscarRyz
For a sound explanation of del, I'd refer to the docs:http://docs.python.org/reference/simple_stmts.html#the-del-statement
fortran
I usually don't read out loud python code xD, but if I had to I think I'd say "slice from the begining to the end of the list l".
fortran
Nice pun in your comment, fortran ... don't know if that was intentional. "How do you read out loud?" ... "For a _sound_ explanation ...".
jellybean
@jellybean completely unintended ^_^ English is not my mother tongue, so I really didn't notice the contrast between the two comments.
fortran
+10  A: 

You could try:

alist[:] = []

Which means: Splice in the list [] (0 elements) at the location [:] (all indexes from start to finish)

The [:] is the slice operator. See this question for more information.

Adam Batkin
good one too :)
fortran
you're shadowing built-in
SilentGhost
I wasn't sure what "you're shadowing built-in" means, but I figured it out. SilentGhost is saying, and I agree, that "list" should not be used as a variable name, because is shadows the "list" type. Many Python developers use "L" as a list variable name, but I prefer "lst"
steveha