Home > Back-end >  Deleting a list first vs. just reassigning it
Deleting a list first vs. just reassigning it

Time:01-07

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

CodePudding user response:

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.

  •  Tags:  
  • Related