emptying a list

J

Jon Clements

Hello

What is faster when clearing a list?

del L[:]

or

L = []

Does it really matter that much?

And you're really talking about two different things, which quite
often come up on this group.

Example follows:
x = range(5)
x = y
print x, y [1, 2, 3, 4] [1, 2, 3, 4]
x = []
print x, y [] [1, 2, 3, 4]
x = y
print x, y [1, 2, 3, 4] [1, 2, 3, 4]
del x[:]
print x, y
[] []

Cheers,
Jon.
 
S

Simon Forman

Hello

What is faster when clearing a list?

del L[:]

or

L = []

The first form actually clears the list, the second for just re-binds
the name 'L' to a new, empty list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top