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.

Example: clear()
cities = ['Mumbai', 'London', 'Paris', 'New York']
print("Before calling clear(): ", cities)
cities.clear()
print("After calling clear(): ", cities)
Output
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.

Example: clear()
randomList = ['Mumbai', 1, (1, 2, 3)]
print("Before calling clear(): ", randomList)
randomList.clear()
print("After calling clear(): ", randomList)
Output
Before calling clear(): ['Mumbai', 1, (1, 2, 3)]
After calling clear(): []
Want to check how much you know Python?