In this article, let’s discuss different ways to clear a list in Python. Python provides a lot of different ways to clear a list and we will discuss them in this article.
Example
Input: [2, 3, 5, 6, 7]
Output: []
Explanation: Python list is cleared and it becomes empty so we have returned empty list.
Different Ways to Remove from 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 methods one by one.
- Using clear()
- Reinitializing the list
- Using “*= 0”
- Using del
- Using pop() method
- Using slicing
Clear a List using Python List clear()
In this example, we are using clear() method to clear a list in Python.
Python3
GEEK = [ 6 , 0 , 4 , 1 ]
print ( 'GEEK before clear:' , GEEK)
GEEK.clear()
print ( 'GEEK after clear:' , GEEK)
|
Output
GEEK before clear: [6, 0, 4, 1]
GEEK after clear: []
Clear a List by 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
list1 = [ 1 , 2 , 3 ]
print ( "List1 before deleting is : "
+ str (list1))
list1 = []
print ( "List1 after clearing using reinitialization : "
+ str (list1))
|
Output
List1 before deleting is : [1, 2, 3]
List1 after clearing using reinitialization : []
Clearing a Python List Using “*= 0”
This is a lesser-known method, but this method removes all elements of the list and makes it empty. In this example, we are using *=0 to clear a list.
Python3
list1 = [ 1 , 2 , 3 ]
print ( "List1 before clearing is : "
+ str (list1))
list1 * = 0
print ( "List1 after clearing using *=0 : "
+ str (list1))
|
Output
List1 before clearing is : [1, 2, 3]
List1 after clearing using *=0 : []
Clearing a List Using del
Python del can be used to clear the list elements in a range, if we don’t give a range, all the elements are deleted. In this example, we are using del keyword to clear a list.
Python3
list1 = [ 1 , 2 , 3 ]
list2 = [ 5 , 6 , 7 ]
print ( "List1 before deleting is : " + str (list1))
del list1[:]
print ( "List1 after clearing using del : " + str (list1))
print ( "List2 before deleting is : " + str (list2))
del 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 : []
Python pop() method To Clear a List
In this example, we are using pop() method to clear a list.
Python3
list1 = [ 1 , 2 , 3 ]
print ( "List1 before deleting is : " + str (list1))
while ( len (list1) ! = 0 ):
list1.pop()
print ( "List1 after clearing using del : " + str (list1))
|
Output
List1 before deleting is : [1, 2, 3]
List1 after clearing using del : []
Time Complexity: O(n^2) where n is the length of the list list1.
Auxiliary Space: O(1).
Clear a List using Slicing
This method involves using slicing to create a new list with no elements, and then assigning it to the original list variable. In this example, we are using slicing to clear a list.
Python3
lst = [ 1 , 2 , 3 , 4 , 5 ]
print ( "List before clearing: " ,lst)
lst = lst[: 0 ]
print ( "List after clearing using Slicing: " ,lst)
|
Output
List before clearing: [1, 2, 3, 4, 5]
List after clearing using Slicing: []
Time Complexity: O(1)
Auxiliary Space: O(n), where n is length of list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Sep, 2023
Like Article
Save Article