Python List clear() - Removes All Items From List
The list.clear()
method removes all the items from the list.
Syntax:
list.clear()
Parameters:
No parameters.
Return value:
None.
The following example demonstrates the clear()
method.
cities = ['Mumbai', 'London', 'Paris', 'New York']
print("Before calling clear(): ", cities)
cities.clear()
print("After calling clear(): ", cities)
Before calling clear(): ['Mumbai', 'London', 'Paris', 'New York']
After calling clear(): []
The clear()
method can clear any type of list. It does not depend on the type of variables in the list.
randomList = ['Mumbai', 1, (1, 2, 3)]
print("Before calling clear(): ", randomList)
randomList.clear()
print("After calling clear(): ", randomList)
Before calling clear(): ['Mumbai', 1, (1, 2, 3)]
After calling clear(): []