- Clear list using inplace operations
- Important effects of inplace clear
- Clear list with standard methods
- Caution during clearing nested lists
Python list object provides an inplace method to clear all elements and return list to empty state. Take care not to assign output to a new variable, as the operation returns None. The method empties out the original list, without need to assign it to a new variable.
Py3: Syntax for clear()
# syntax inplace clear list.clear() --> None #---EXAMPLE: empty out a list pylist = [1, 2, 3] pylist.clear() print(pylist) #=  #---EXAMPLE: check the return value # clear does not return a list object pylist = [1, 2, 3] return_value = pylist.clear() print(return_value) #= None
Other inplace methods exist to clear a list.
Py3: Alternate clearing of list
# methods to clear a list; all inplace del x[:] x[:] =  # different from x= x *= 0 # quite fast
Issues arise when nested list elements are deleted inplace. This are traits of Python, and should be properly understood. The effects are illustrated using the built-in function id(obj), which returns an integer unique to an object.
Py3: Inplace clearing for references
# original list x = [1, 2, 3] # second list is a reference to original # Python just creates a pointer to 'x' # not a new copy y = x # check ids for x and y: they are same # so x and y are like pointers to same object x_id = id(x) #= 26438763 y_id = id(y) #= 26438763 print(x) #= [1,2,3] print(y) #= [1,2,3] # what happens when one is cleared? x.clear() # original memory location is cleared # so all references are nullified print(x) #=  print(y) #=  # x and y are still pointing to same object x_id = id(x) #= 26438763 y_id = id(y) #= 26438763
Notes: Clearing one clears all references
Both variables x and y point to the same list object. When one has all the elements removed, the other referenced variable which maintains the link also shows an empty list. We can consider x and y as pointers in C++. So y=x assignment is by reference to object id, and not a copy of its values. This is true for lists and other mutable objects in Python.
Instead of inplace methods, when standard operations operate on objects, they create a shallow copy. This is restricted to the first level of elements. The new variable has uniquely different identity.
Py3: Standard operations
# original list x = [1,2,3] # second list referenced to original # this is like a pointer to object y = x # x and y have same indented x_id = id(x) #= 26438763 y_id = id(y) #= 26438763 print(x, y) #= [1,2,3] [1,2,3] # two standard methods of clearing list # assign a new empty list x =  x = list() # now only x is cleared, it is a new list object # y retains old values print(x, y) #=  [1,2,3] # identities of x and y are different x_id = id(x) #= 67394754 y_id = id(y) #= 26438763
Notes: Standard methods to clear lists
Assigning a list to a new variable just copies the reference identity and so they point to the same object. This is shown earlier. However when one variable has an empty list assigned to it, a brand new identity come along with it. This decouples x and y. So y keeps pointing to original object, while x is assigned a brand new, but empty list object.
Since inplace operations make a shallow copy, nested elements can be altered causing unintended effects.
Py3: Nested list clearing
# original list with nested element x = [1,] # referencing first element into new list # clear reference to first element y = x[0:1] #=  y.clear() #=  # does this change x? # no effect print(x) #= [1, ] # referencing the second nested element # clear the reference z = x #=  z.clear() #=  # original object is altered print(x) #= [1, ]
Notes: Mindful of nested elements
When first level elements are shallow copied, they are unique. So modifying one inplace does not affect the other. However nested elements are just copied by reference. So clearing the new reference variable, affects the original.
It is better to use standard methods of assigning an empty list to reset a list to empty state. Inplace operations can cause unintended soft errors. Be mindful while assigning segments of a list to another reference variable, remembering that the assignment may copy values, or may just create a pointer.