Skip to content

How to clear a list in Python?

To clear/empty a list in Python, we can use the predefined clear() method.

lst = [1,2,3]
print(lst)
#[1, 2, 3]
lst.clear()
print(lst)
#[]

Another way to empty a list is by using slicing.

lst = [1,2,3]
lst[:] = []
print(lst)
#[]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.