Tuesday, December 15, 2015

clear list for further use

http://stackoverflow.com/questions/850795/clearing-python-lists


just reinitialize as:
>>> a = [1, 2, 3]
>>> b = a
>>> a = []
>>> print(a)
[]
>>> print(b)
[1, 2, 3]

There is other method to delete a list but it has its own issues:

>>> a = [1, 2, 3]
>>> b = a
>>> del a[:]      # equivalent to   del a[0:len(a)]
>>> print(a)
[]
>>> print(b)
[]
>>> a is b
True
You could also do:

>>> a[:] = []

No comments:

Post a Comment