Python List clear()
In python list, the clear() method removes all the elements from the given list.
Python List clear()
The syntax of clear() method is:
1
2
list.clear()
clear() parameters
The clear method does not take any parameters as arguments.
Let see an example of the python list clear() method.
Example 1: How to use clear() method on python list.
1
2
3
4
5
6
7
8
9
10
my_cars = ["AUDI","BMW","FORD"]
print(my_cars)
#clearing the list using clear method
my_cars.clear()
print("List after clear method:", my_cars)
The output will be as follow:
1
2
3
['AUDI', 'BMW', 'FORD']
List after clear method: []
Rules of clear()
- The clear() method does not return any value.
- The clear() method only works with python 3.3 + versions, in python 2 and python 3.2 del method is used.
This post is licensed under CC BY 4.0 by the author.