![]() |
VOOZH | about |
clear() method is used to remove all elements from a list. It empties the list completely while keeping the original list object unchanged.
In this example, clear() removes all items from the list.
[]
Explanation: clear() removes all elements from list a, making it an empty list.
list.clear()
Example 1: In this example, the list contains sublists. Calling clear() removes all nested elements from the main list.
[]
Explanation: clear() removes all sublists stored inside list a.
Example 2: Here, the list is cleared and then reused to store new values without creating a new list.
[40]
Explanation: After clear() empties the list, append(40) adds a new element to the same list.