clear() to remove all items in a list

All list methods

clear() takes no argument.

Output ( all items removed )
We can use del also to remove all the elements of the list. We can check the data type after using del.
del my_list[:]
<class 'list'>

Difference between del and clear

clear is a method of list class, del is a keyword in Python. After using clear the list is not deleted but after using del the list object is deleted.
del my_list
print(my_list)  #NameError
The last line will generate error as we can't print the list after deleting the same.

All list methods Questions with solutions on List pop() to remove item based on position
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer