Clear a list Python
This post will discuss how to remove all items from the list in Python. In other words, empty or delete a list in Python. 1. Using list.clear() functionlist.clear() is the recommended solution in Python 3 to remove all items from the list.
DownloadRun Code 2. Using Slice assignmentYou can empty the list by replacing all the elements with an empty list. But simply doing a = [] wont clear the list. It just creates a new list and binds it to the variable a, but the old list and its references remain in memory. For instance,
DownloadRun Code
DownloadRun Code 3. Using del statementYou can also use the del statement to delete the contents of an entire list. This is demonstrated below:
DownloadRun Code Thats all about removing all items from a list in Python. |