In this post, we’ll learn how to use Python list clear method. We’ll be using multiple practical code examples for detailed demonstration.
I hope after reading this article, you’ll have a complete knowledge of how to clear Python list.
Let’s nowt wait anymore and just get right into its practical implementation.
- What is Python List Clear Method?
- Syntax of Clear Method
- Implementing Python List Clear Method (Multiple Easy Examples)
- Example 1: Clear Method on a Simple List
- Example 2: Clear Method on Complex Nested List
- Example 3: Clear Method Returns None
- Example 4: Use del to Clear the List
What is Python List Clear Method?
It is a method that is used to clear/remove all the items from Python list.
Let’s now first discuss its syntax, then we will understand its implementation using multiple examples.
Syntax of Clear Method
This is how you can use this method on Python list. It does not take any arguments.
Implementing Python List Clear Method (Multiple Easy Examples)
See the below examples to understand how to properly apply clear method on Python list.
Example 1: Clear Method on a Simple List
listOfIntegers=[1,3,5,5,6,7,73,2] listOfIntegers.clear() print(listOfIntegers) // 
You can see that we have first defined a simple list. Then we’ve applied the clear method on it. As a result, the list is now empty.
Example 2: Clear Method on Complex Nested List
complexList=[1,3,[3,5],5,5,('red',5.6),6,'Green',7.5,7,73,2] complexList.clear() print(complexList) // 
We have populated our list with integers, nested list, tuple, float and string to give you a proper idea that the clear method works on it as well.
You can see that we got an empty list after the clear method is applied on it.
Example 3: Clear Method Returns None
listOfValues=[3,5,6,6.7,24.8] newVar=listOfValues.clear() print(newVar) // None
We have declared a new variable and passed the list on which the clear method is applied. This variable will store if something is returned by the clear method. As you can see, it shows none which means the clear method does not return anything, it just clears the current list.
Example 4: Use del to Clear the List
We can make use of the Python’s del keyword to clear the list. See below code:
listOfValues=[3,5,6,6.7,24.8] del listOfValues print(listOfValues) // NameError: name 'listOfValues' is not defined
You can see that applying del directly on the list will delete the list. As a result, we are seeing a name error that this specific list is not defined.
Let’s see how to remove the items of list using del. See below code:
listOfValues=[3,5,6,6.7,24.8] del listOfValues[:] // removes all the items of list as no specific range is defined print(listOfValues) // 
We can make use of this slicing method to remove all items of list using the del keyword.
So this is how you can easily implement Python list clear method in your own code. Hope you like this post.
Do ask if you still have any questions regarding the use of Python list clear method. I’d be happy to answer all your questions.
To conclude, hope you now have a proper idea of how to use Python list clear method as we have discussed its role and implementation with practical examples in this post. I would be happy to have your feedback. Thank you for reading this post.