How to empty a list in Python

We have seen how to take inputs to a list. Now we will see how to empty a list in Python. There are many ways for doing this. Let’s go through every method.
Using clear()
This built-in function empty the list completely.
li = [4, 34, 24, 5, 11] print(li) li.clear() print(li)
Output:
[4, 34, 24, 5, 11] []
Using del()
The del() function selectively remove items at a given index or can used to remove all the elements, making the list empty.
li = ['a', 'e', 'i', 'o', 'b', 'u'] print(li) #deleting one element at a specified index from the list del li[4] print(li) #removing all the elements del li[:] print(li)
Output:
['a', 'e', 'i', 'o', 'b', 'u'] ['a', 'e', 'i', 'o', 'u'] []
Subscribe
Login
Please login to comment
0 Discussion