The clear() method in Python is used to remove all the elements or items from a list. The clear() method does not take any arguments and return any value. It simply removes all items from the list and makes it empty. In this article, I will explain the Python list clear() method and using its syntax, parameters, and usage how we can remove all the elements from a given list with examples.
1. Quick Examples of List Clear() Method
If you are in a hurry, below are some quick examples of the list clear() method.
# Quick examples of list Clear() method
# Initialize list
mylist = [10, 20, 30, 40, 50]
# Example 1: Clear a list in python
mylist.clear()
# Example 2: Clear list using del()
del mylist[:]
# Example 3: Clear list
# Using pop() method
while(len(mylist) != 0):
mylist.pop()
# Example 4: Using pop() method
while mylist:
mylist.pop()
# Example 5: Clear list
# Using the slicing
mylist = mylist[:0]
# Example 6: List Re-Initialization
mylist = []
# Example 7: Using *= 0
mylist *= 0
2. Python List clear() Method
list.clear() method is a in-built of Python which is used to clear the list of all elements at a time and make the given list empty. This is one of the best method of Python when we want to clear all items at a time.
2.1 Syntax of List clear() Method
Following is the syntax of the list clear() method
# Syntax of clear() function
list.clear()
2.1 Parameter of list clear()
The clear() method does not take any parameter.
3. Clear a List in Python
You can use the clear() method to remove all the elements from a list. For example, create a list of values and call the clear() method, which will remove all the elements from the list. After that, call the print() function it will show the empty on the console.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print(“Original list: “, mylist)
# Clear a list using clear()
mylist.clear()
print(“After clearing the list: “, mylist)
Yields below output.
4. Clear List Using del() Statement
You can also delete single/multiple elements from a list using the del statement in Python. If you don’t specify a range, it will delete all the elements in the list, effectively and make the list clear. For example, using slicing technique specify the portion of elements of given list named mylist and call del statement, it will delete the specified portion of elements from a list.
If you don’t specify any range in slicing it will consider all elements and if you specify with range it will consider particular portion of elements. For example, del mylist[1:3]it would be delete the second and third elements in the list.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print(“Original list: “, mylist)
# Clear list using del()
del mylist[:]
print(“After clearing the list: “, mylist)
Yields the same output as above.
5. Clear List using pop() method
You can also clear a list using the pop() method in Python, is used to remove each element from a list one by one. Use pop() method to remove each element from the list one by one with the help of loop over the list.
In the below program, the while loop runs as long as the length of mylist is not equal to 0. This means that the loop will continue until all elements of the list have been removed. Inside the loop, the pop() method is called on mylist, which removes and returns the last element of the list. The loop continues to run until mylist is empty.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print(“Original list: “, mylist)
# Clear list using pop() method
while(len(mylist) != 0):
mylist.pop()
print(“After clearing the list: “, mylist)
# Using pop() method
while mylist:
mylist.pop()
print(“After clearing the list: “, mylist)
Yields the same output as above.
6. Clear List Using the Slicing
You can also clear the list using the slicing technique and print the empty list. For example, clears the list using slicing. It creates a new list that starts at index 0 and ends at index 0 (which is an empty range) and assigns it to the variable mylist. This effectively empties the list.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print(“Original list: “, mylist)
# Clear list Using the slicing
mylist = mylist[:0]
print(“After clearing the list: “, mylist)
Yields the same output as above.
7. Using List Re-Initialization
You can clear a list using list re-initialization in Python by assigning an empty list to the variable that holds the old list. In the below code, the variable mylist is assigned an empty list object [], effectively clearing the list.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print(“Original list: “, mylist)
# Using list Re-Initialization
mylist = []
print(“After clearing the list: “, mylist)
Yields the same output as above.
8. Clear List Using *=0 Operator
You can clear a list using the *= 0 operator by multiplying the list with 0. For example, the *= 0 operator is used to clear the list. When the list is multiplied by 0, it effectively becomes an empty list.
# Initialize list
mylist = [10, 20, 30, 40, 50]
print(“Original list: “, mylist)
# Using *= 0 this syntax clear the list
mylist *= 0
print(“After clearing the list: “, mylist)
Yields the same output as above.
9. Conclusion
In this article, I have explained the Python list clear() method and using its syntax, parameter, and usage how to remove all the elements from a given list with examples. This method doesn’t take any arguments and returns nothing but removes the elements from the list.
Happy Learning !!
The clear() method in Python is used to remove all the elements or items from a list. The clear() method does not take any arguments and return any value. It simply removes all items from the list and makes it empty. In this article, I will explain the Python list clear() method and using its Read More Python, Python Tutorial, Python List Methods