Python list Method - clear()

htt‮sp‬://www.theitroad.com

The clear() method in Python is a built-in method used to remove all the elements from a list.

Syntax:

list_name.clear()

Example:

my_list = [1, 2, 3, 4]
my_list.clear()
print(my_list)   # Output: []

The clear() method modifies the original list and does not return anything. It is useful when we want to remove all the elements from a list. If we want to remove a specific element from the list, we can use the remove() method or use slicing to remove a range of elements from the list.