In this article, we will learn to clear the elements of the list using the python program.
We clear the list using four methods:
- Using clear() function
- Using del method
- Using "*=" method
Using clear() function
Explanation
Clear() is the function that clears all the list elements.
Program
1
2
3
4
5
6
7
8 | # List
List = [1, 2, 3, 4, 5, 6]
# clear the list using clear() function
List.clear()
# print the list
print(List)
|
Output
Using del method
Explanation
The del() function removes the selective elements in the list at a given index in the list.
Program
1
2
3
4
5
6
7
8 | # List
List = [1, 2, 3, 4, 5, 6]
# clear the list using del() function
del List[:]
# print the list
print(List)
|
Output
Using "*=0" method
Explanation
This method assigns 0 to all the elements in the list which make the list empty.
Program
1
2
3
4
5
6
7
8 | # List
List = [1, 2, 3, 4, 5, 6]
# clear the list using "*=0"
List *= 0
# print the list
print(List)
|
Output