Search code examples
pythonpython-3.xgarbage-collection

Deleting a list first vs. just reassigning it


When I have a list I need to clear, I usually just reassign it to a new empty list but is that the correct way or should I 'del' it first

example:

mylist = [1,2,3,4,5]
mylist = []

or

mylist = [1,2,3,4,5]
del mylist
mylist = []

Regards


Solution

  • TL;DR: the first one is good, no need to del first.

    Both examples end up in the same exact situation: there's the original list object, which has 0 references to it, and there's a new list object which is bound to the name mylist.