Python List – clear()

Python list.clear() method removes all the elements from the list.

clear() method modifies the original list.

Syntax

The syntax to call clear() method on a list myList is

myList.clear()

where

  • myList is a Python list
  • clear is method name
ADVERTISEMENT

Examples

Remove all Elements from List

In the following program, we initialize a list myList with some elements, and remove all the elements from the list using clear() method.

main.py

#take a list
myList = ['apple', 'banana', 'cherry']

#remove all elements from the list
myList.clear()

print(f'myList : {myList}')
Try Online

Output

myList : []

Conclusion

In this Python Tutorial, we learned how to clear a list (delete all elements) using list.clear() method.