Python List clear() Method

Python List clear

Now, we are going to learn about the clear method. As the name of the method says, this method removes all the items from the list. Let’s have a look at a simple program, which tries to demonstrate the clear method –

Python List clear

As you can see in the above program, we have a list, with some numbers, and then we are trying to use the clear method on the list. The clear method removes all the elements from the list, and when we are printing the list, it is simply empty. You can also check the output of the above program.

Alternatively, you can also use the del keyword to empty the list. Have a look at the below program, which demonstrates using the del keyword to empty the list.

As you can see, we have made use of the del keyword, to clear the list. You can observe the output of this program as well, and you will find that the list that we get is empty list. So, we can make use of the clear method, as and when required in our python programs.