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

Single Inheritance, multiple inheritance and multilevel inheritance in python.

Git Commands.

Using class objects, Instance variables, and class variables, class methods and static method in python