The Python list clear() method removes all the elements from the list. It clear the list completely and returns nothing. It does not require any parameter and returns no exception if the list is already empty.
The following is the syntax of the list clear() method:
Let's see some examples of list clear() method to understand it's functionality.
Let's see a simple example in which clear() method is used to clear a list. The clear() method clears all the elements from the list.
Output:
Given List: ['1', '2', '3'] Updated List: []
Explanation:
In the above example, we are given a list. We used the clear() method to remove all the elements from the list. As a result, the list becomes empty.
If the list is already empty, the method returns nothing. See the example below.
Output:
Given List: [] Updated List: []
Explanation:
In this example, we are given an empty list. We used the clear() method to clear the list. Since, the list is already empty, there is no impact on the list after using clear(). Hence, it returns nothing.
We request you to subscribe our newsletter for upcoming updates.