Python3 list clear() method

Brief introduction

python list’s clear() function method is used to clear the entire list (list), the role is the same as del lis[:], but it is different from the list’s remove method, the former is to clear the latter is to remove an element of the list.


The statement syntax of the clear() method is as follows.

lis.clear() # lis is the list object you want to manipulate, then .clear() is a call to the list's own clear list method.

Passing parameters and return values

This method does not need to pass parameters, directly empty the list list all elements, also does not have any return value, the list is a variable data type inside python, directly do in situ modification.


The following code demonstrates the use of the Python3 list clear() method.

>>> lis = ['abc', 'bbb', 'cnpyhton', 123]
>>> lis.clear()
>>> print ("View the contents of the emptied list : ", lis)

We end up with a blank empty list object using the empty list method.

Leave a Reply