Different ways to clear a list in Python

17 Oct, 2019
There are many ways of clearing the list through methods of different constructs offered by Python language. Let’s try to understand each of the method one by one.
Method #1 : Using clear() method# Python program to clear a list# using clear() method   # Creating listGEEK = [6, 0, 4, 1]print('GEEK before clear:', GEEK)   # Clearing list GEEK.clear() print('GEEK after clear:', GEEK) Output:GEEK before clear: [6, 0, 4, 1]
GEEK after clear: []
 
# Python program to clear a list# using clear() method   # Creating listGEEK = [6, 0, 4, 1]print('GEEK before clear:', GEEK)   # Clearing list GEEK.clear() print('GEEK after clear:', GEEK) 
Output:
GEEK before clear: [6, 0, 4, 1]
GEEK after clear: []

 
Method #2 : Reinitializing the list : The initialization of the list in that scope, initializes the list with no value. i.e list of size 0. Let’s see the example demonstrating Method 1 and 2 to clear list# Python3 code to demonstrate # clearing a list using# clear and Reinitializing   # Initializing listslist1 = [1, 2, 3]list2 = [5, 6, 7]  # Printing list1 before deleting print ("List1 before deleting is : " +  str(list1))  # deleting list using clear()list1.clear()  # Printing list1 after clearingprint ("List1 after clearing using clear() : " + str(list1))  # Printing list2 before deleting print ("List2 before deleting is : " +  str(list2))  # deleting list using reinitializationlist2 = []  # Printing list2 after reinitializationprint ("List2 after clearing using reinitialization : " + str(list2))Output:List1 before deleting is : [1, 2, 3]
List1 after clearing using clear() : []
List2 before deleting is : [5, 6, 7]
List2 after clearing using reinitialization : []
 
# Python3 code to demonstrate # clearing a list using# clear and Reinitializing   # Initializing listslist1 = [1, 2, 3]list2 = [5, 6, 7]  # Printing list1 before deleting print ("List1 before deleting is : " +  str(list1))  # deleting list using clear()list1.clear()  # Printing list1 after clearingprint ("List1 after clearing using clear() : " + str(list1))  # Printing list2 before deleting print ("List2 before deleting is : " +  str(list2))  # deleting list using reinitializationlist2 = []  # Printing list2 after reinitializationprint ("List2 after clearing using reinitialization : " + str(list2))
Output:
List1 before deleting is : [1, 2, 3]
List1 after clearing using clear() : []
List2 before deleting is : [5, 6, 7]
List2 after clearing using reinitialization : []

 
Method #3 : Using “*= 0” : This is a lesser known method, but this method removes all elements of the list and makes it empty.# Python3 code to demonstrate # clearing a list using# *= 0 method  # Initializing listslist1 = [1, 2, 3]  # Printing list1 before deleting print ("List1 before deleting is : " + str(list1))  # deleting list using *= 0list1 *= 0  # Printing list1 after *= 0print ("List1 after clearing using *= 0: " + str(list1))Output:List1 before deleting is : [1, 2, 3]
List1 after clearing using *= 0: [] 
# Python3 code to demonstrate # clearing a list using# *= 0 method  # Initializing listslist1 = [1, 2, 3]  # Printing list1 before deleting print ("List1 before deleting is : " + str(list1))  # deleting list using *= 0list1 *= 0  # Printing list1 after *= 0print ("List1 after clearing using *= 0: " + str(list1))
Output:
List1 before deleting is : [1, 2, 3]
List1 after clearing using *= 0: []
 
Method #4 : Using del : del can be used to clear the list elements in a range, if we don’t give a range, all the elements are deleted.# Python3 code to demonstrate # clearing a list using# del method  # Initializing listslist1 = [1, 2, 3]list2 = [5, 6, 7]  # Printing list1 before deleting print ("List1 before deleting is : " + str(list1))  # deleting list1 using deldel list1[:]print ("List1 after clearing using del : " + str(list1))    # Printing list2 before deleting print ("List2 before deleting is : " + str(list2))  # deleting list using deldel list2[:]print ("List2 after clearing using del : " + str(list2))Output:List1 before deleting is : [1, 2, 3]
List1 after clearing using del : []
List2 before deleting is : [5, 6, 7]
List2 after clearing using del : []
My Personal Notes
arrow_drop_upSave
# Python3 code to demonstrate # clearing a list using# del method  # Initializing listslist1 = [1, 2, 3]list2 = [5, 6, 7]  # Printing list1 before deleting print ("List1 before deleting is : " + str(list1))  # deleting list1 using deldel list1[:]print ("List1 after clearing using del : " + str(list1))    # Printing list2 before deleting print ("List2 before deleting is : " + str(list2))  # deleting list using deldel list2[:]print ("List2 after clearing using del : " + str(list2))
Output:
List1 before deleting is : [1, 2, 3]
List1 after clearing using del : []
List2 before deleting is : [5, 6, 7]
List2 after clearing using del : []

nikhilaggarwal3
Python list-programs
python-list
python-list-functions
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON