Program to clear the list using different methods using Python

by | Jan 23, 2021 | Python Programs

Introduction

In this section of python programming, we will discuss the different ways to clear the list. Python provides four ways to clear :

  • Using clear(): Clear the list to size zero (empty list)
  • Using reinitialization: Reinitializing the list with empty value
  • Using *=0: Removes all the element
  • Using delete statement: Deletes all the values if no range is provided (i.e. list[:])

 

Program to clear the list using different methods

Program

list1 = ['is', 'at', 'around', 'in', 'the' ]
list2 = ['red', 'blue', 'black', 'green', 'grey']
list3 = [1, 2, 3, 4, 5]
list4 = [6, 9, 10, 7, 11]
# Using clear
print("Elements in list1: ",list1)
list1.clear()
print("Elements after using clear():",list1)
#Reinitialization
print("Elements in list2: ",list2)
list2 = []
print("Elements after using reinitialization:",list2)
# Using *= 0
print("Elements in list3: ",list3)
list3 *= 0
print("Elements after using *= 0:", list3)
# Using del
print("Elements in list4: ",list4)
del list4[:]
print("Elements after using del:",list4)

Output

Program to clear the list using different methods Output

Explanation

In the above python code we have created four lists with different values. We have used the different ways (mention above) to clear the list.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Go Coding