xxxxxxxxxx
# list of cars
cars = ['Benz',('volkswagen','BMW'),'Ford','Ferrari',('volkswagen','BMW')]
numbers= [1,(1,3),5,7,(1,3),3,1,6,(1,3)]
# clear the car list
cars.clear()
print("After clearing the car list = ",cars)
# clear the number list
numbers.clear()
print("After clearing the number list = ",numbers)
xxxxxxxxxx
your_list = [1,2,3,4,5,6,7,8,9,10]
your_list.clear() #List becomes [] empty
xxxxxxxxxx
# this clear whole elements from list
thislist = ["apple", "banana", "cherry"]
thislist.clear()
xxxxxxxxxx
# clear list
my_list = [ 11, 22, 33, 44, 55 ]
my_list.clear()
print( my_list ) # [ ]
xxxxxxxxxx
l = ["a", "b", "c", "d", "e"]
for x in range(0, len(l)):
if(len(l) == 0):
print("Testing!")
#add a break here is you want the for loop to end after printing "Testing"
l.clear()
print(len(l))
#after you clear the list, the list is empty and len(l) will now equal 0. But the original range will stay the same
#hence the for loop will continue even though the list has changed in size
xxxxxxxxxx
# list of cars
cars = ['Benz',('volkswagen','BMW'),'Ford','Ferrari',('volkswagen','BMW')]
numbers= [1,(1,3),5,7,(1,3),3,1,6,(1,3)]
# deletes all the elements in the car list
del cars[:]
print("After clearing the car list = ",cars)
# deletes all the elements in the number list
del numbers[:]
print("After clearing the number list = ",numbers)
xxxxxxxxxx
my_list = [1, 2, 3, 4, 5]
# Clear the list using the clear() method
my_list.clear()
print(my_list) # Output: []