what is the difference between del a[:] and a = [] when I want to empty a list called a in python? -


this question has answer here:

please efficient way of emptying list?

i have list called a = [1,2,3]. delete content of list write a = [ ]. came across function in python called del. want know if there difference between del [:] , use.

there difference, , has whether list referenced multiple places/names.

>>> = [1, 2, 3] >>> b = >>> del a[:] >>> print(b) []  >>> = [1, 2, 3] >>> b = >>> = [] >>> print(b) [1, 2, 3] 

using del a[:] clears existing list, means anywhere it's referenced become empty list.

using a = [] sets a point new empty list, means other places original list referenced remain non-empty.

the key understanding here realize when assign variable, makes name point thing. things can have multiple names, , changing name points doesn't change thing itself.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -