python
Remove all items from a list in Python
If you want to remove all items or elements from a list then you can use List.clear() function. It will empty the list in Python.
my_list = [10, 20, 30, 40, 50]
my_list.clear()
print(my_list)
Output
[]
Python List.clear() function
In Python List.clear() function is used to remove all the items from the list. It is a function of the list and you can not call this method without using the list variable name.
Syntax
List.clear()
Code Example
items = ["a", "b", "c", "d"]
items.clear()
print(items)
Output
[]
Remove all items from the list using del keyword
You can also use the del keyword to delete all the elements from the list. You just need to pass the beginning and the last index of the list to delete all the items.
Syntax to delete all items from the list
del list[start_index:last_index+1]
Code Example
numbers = [10, 20, 30, 40]
del numbers[0:4]
print(numbers)
Output
[]
Or you can also use the below code where we are using len() function.
numbers = [10, 20, 30, 40]
del numbers[0:len(numbers)]
print(numbers)
Output
[]
Was this helpful?
Similar Posts
- Remove items less than a specific value from Python list
- Remove duplicate items from a list in Python
- Python - regex , remove all single characters,replace all single chars,char
- Pandas - Remove duplicate items from list
- String characters to list items using python
- Add two list items with their respective index in python
- Add items to list in Python