Reassigning list to an empty list

You can clear a list in Python by reassigning the list to an empty list. For example:

my_list = [1, 2, 3, 4, 5]
print("Before clearing:", my_list)
my_list = []
print("After clearing:", my_list)

Output:

Before clearing: [1, 2, 3, 4, 5]
After clearing: []

Using clear() in Python 3.3

Another way to clear a list is to use the clear() method, which is available in Python 3.3 and later:

my_list = [1, 2, 3, 4, 5]
print("Before clearing:", my_list)
my_list.clear()
print("After clearing:", my_list)

Output:

Before clearing: [1, 2, 3, 4, 5]
After clearing: []