Different ways to clear a list in Python
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# list of lettersGEEK=[6,0,4,1]print('GEEK before clear:', GEEK)# clearing vowelsGEEK.clear()print('GEEK after clear:', GEEK)chevron_rightfilter_noneOutput:
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 deletingprint("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 deletingprint("List2 before deleting is : "+str(list2))# deleting list using reinitializationlist2=[]# Printing list2 after reinitializationprint("List2 after clearing using reinitialization : "+str(list2))chevron_rightfilter_noneOutput:
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 deletingprint("List1 before deleting is : "+str(list1))# deleting list using *= 0list1*=0# Printing list1 after *= 0print("List1 after clearing using *= 0: "+str(list1))chevron_rightfilter_noneOutput:
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 deletingprint("List1 before deleting is : "+str(list1))# deleting list1 using deldellist1[:]print("List1 after clearing using del : "+str(list1))# Printing list2 before deletingprint("List2 before deleting is : "+str(list2))# deleting list using deldellist2[:]print("List2 after clearing using del : "+str(list2))chevron_rightfilter_noneOutput:
List1 before deleting is : [1, 2, 3] List1 after clearing using del : [] List2 before deleting is : [5, 6, 7] List2 after clearing using del : []
Recommended Posts:
- Python | Ways to sum list of lists and return sum list
- Python | Ways to Convert a 3D list into a 2D list
- Python | Ways to Spilt the list by some value
- Python | Ways to shuffle a list
- Python | Ways to flatten a 2D list
- Extending a list in Python (5 different ways)
- Python | Ways to rotate a list
- Python | Ways to merge strings into list
- Python | Ways to initialize list with alphabets
- Python | Ways to format elements of given list
- Python | Ways to find length of list
- Python | Ways to remove duplicates from list
- Python | Ways to remove particular list element
- Python | Ways to find indices of value in list
- Python | Multiply all numbers in the list (3 different ways)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



