clear() to remove all items in a list

clear() takes no argument.

my_list=['Alex','Ronald','John','Ronald','Ronn']
my_list.clear()
print(my_list)
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.
my_list=['Alex','Ronald','John','Ronald','Ronn']
del my_list[:]
print(my_list)  
print(type(my_list))
Output
[]
<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.
my_list=['Alex','Ronald','John','Ronald','Ronn']
del my_list
print(my_list)  #NameError
The last line will generate error as we can't print the list after deleting the same.

Example 1: Clearing a List After Processing

my_list = [4, 5, 6]
print("Before clearing:", my_list)  # Output: [4, 5, 6]
my_list.clear()
print("After clearing:", my_list)   # Output: []

Example 2: Using clear() with Nested Lists

nested_list = [[1, 2], [3, 4], [5, 6]]
nested_list[0].clear()
print(nested_list)  # Output: [[], [3, 4], [5, 6]]

Example 3: Resetting List in a Function

def reset_list(my_list):
    my_list.clear()

data = [1, 2, 3]
reset_list(data)
print(data)  # Output: []

All list methods Questions with solutions on List pop() to remove item based on position
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







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 Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer