Different ways to clear a list in python.

 Different ways to clear a list in python

Code - 

--------------------------------------

# Different ways to clear a list in python

# Method 1
list3 = ["anant", "shiv", "hariom", "utsav", "shorya", "ajay", "carry", "harry"]


def list_cleaner(list):
list.clear()


print("Before: ", list3)
list_cleaner(list3)
print("After: ", list3)

# Method 2

list4 = ["anant", "shiv", "hariom", "utsav", "shorya", "ajay", "carry", "harry"]

print("Before: ", list4)
list4 = []
print("After: ", list4)

# Method 3
list3 = ["anant", "shiv", "hariom", "utsav", "shorya", "ajay", "carry", "harry"]



def list_cleaner5(list):
"""This function will clear the list you put as an argument.."""
# list.clear()
for i in range(len(list)):
list.pop()


print("Before: ", list3)
list_cleaner5(list3)
print("After: ", list3)
# print(list_cleaner.__doc__)


Comments

Popular posts from this blog

Shopping Bill Generator In python.

Should we start preparation for BCA/MCA from class 11th ? How ?

Python program to check that a list have elements in it or not .