In this section, we will learn how to empty a list in Python.
Python List clear() Method
The Python List clear() method is used to remove the entire elements of a list. This means after running this method on top a list object, its entire elements will be gone.
List clear() Method Syntax:
List.clear()
List clear() Method Parameter:
The method does not take any argument.
List clear() Method Return Value:
The method does not return a value.
It will affect the target list by removing its elements. But again, it doesn’t return a value.
Example: empty list in python
names = ["Omid", "Jack", "John"] names.clear() print(names)
Output:
[]
Example: python clear a list
list1 = [1,2,3,4,5,6,7,8,9] list1.clear() print(list1)
Output:
[]