my_list = [10, 20, 30, 40, 50]
my_list.clear()
print(my_list)
Output
[]
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
[]
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
[]
0 Comments