How to clear Python list

Time:2021-1-25

Operation of situation list:

del list[:]

list=[]

list[:]=[]


def func(L):                                  
  L.append(1)
  print L
  #L[:]=[]
  #del L[:]
  L = []
  print L
 
L=[]
func(L)
print L

Output results:

[1]

[]

[1]

Analysis: l is a variable data type. As a parameter, the change of L in the function can be reflected in the L outside the function. Executing L. append (1) is the memory occupied by L outside the function, and then executing L = [], (L inside the function) when l points to another space outside the function. So, func (L), print L, output [1].

In fact, the original intention of the function is to clear the memory pointed by the parameter L. using L = [] does not clear the memory pointed by L


def func(L):
  L.append(1)                                 
  print L
  L[:]=[]
  #del L[:]
  #L = []
  print L
 
L=[]
func(L)
print L

Output results:


[1]
[]
[]

L [:] =]: clear the memory corresponding to L


def func(L): 

  L.append(1)
  print L
  #L[:]=[]
  del L[:]
  #L = []
  print L
 
L=[]
func(L)
print L

analysis:

The effect of del l [:] is the same as that of L [:] =].

Python assignment is often done by pointer, a = B, just let a point to B, not copy the content of B to a


def func(L):                                  
  L.append(1)
  print L
  print id(L)
  #L[:]=[]
  #del L[:]
  L = []
  print id(L)
  print L
 
L=[]
func(L)
print L

Output results:

31460240

31460168

Obviously: after assigning L = [], the memory that l points to is completely inconsistent.

Similar to C + + reference assignment.

Python assignments are all reference assignments, which is equivalent to another example of using pointers


list =[]                                    
next = [None,None]
for i in range(10):
  next[0] = i 
  #print id(i)
  #print id(next[0])
  next[1] = i 
  #print id(next)
  list.append(next)
 
print list

Output results:

[[9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9]]

It’s not what we want

list.append (next), just put the next address in the list

We use a next in the whole for loop, but every time we use the for loop, we operate on the initial next. This operation will cover the last result


list =[]                                    
next = [None,None]
for i in range(10):
  next[0] = i 
  #print id(i)
  #print id(next[0])
  next[1] = i 
  #print id(next)
  list.append(next)
 
print list
print id(list[0])
print id(list[1])

Output results:

[[9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9], [9, 9]]

36166472

36166472

The solution is to reallocate space every time the for loop


list =[]                                    
for i in range(10):
  next = [None,None]
  next[0] = i 
  #print id(i)
  #print id(next[0])
  next[1] = i 
  #print id(next)
  list.append(next)
 
print list
print id(list[0])
print id(list[1])

Output results:

[[0, 0], [1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7], [8, 8], [9, 9]]

15060360

15059712

The way to clear the python list above is the whole content shared by Xiaobian. I hope it can give you a reference and support developer.