Different ways to clear a list in Python
Last Updated :
26 Mar, 2024
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
- using list comprehension
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)
# Clearing list
GEEK.clear()
print('GEEK after clear:', GEEK)
OutputGEEK 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]
# Printing list2 before deleting
print("List1 before deleting is : "
+ str(list1))
# deleting list using reinitialization
list1 = []
# Printing list2 after reinitialization
print("List1 after clearing using reinitialization : "
+ str(list1))
OutputList1 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
# Initializing lists
list1 = [1, 2, 3]
# Printing list2 before deleting
print("List1 before clearing is : "
+ str(list1))
list1*=0
# Printing list2 after reinitialization
print("List1 after clearing using *=0 : "
+ str(list1))
OutputList1 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]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list1 using del
del list1[:]
print("List1 after clearing using del : " + str(list1))
# Printing list2 before deleting
print("List2 before deleting is : " + str(list2))
# deleting list using del
del list2[:]
print("List2 after clearing using del : " + str(list2))
OutputList1 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]
# Printing list1 before deleting
print("List1 before deleting is : " + str(list1))
# deleting list1
while(len(list1) != 0):
list1.pop()
print("List1 after clearing using del : " + str(list1))
OutputList1 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
# Initializing list
lst = [1, 2, 3, 4, 5]
print("List before clearing: ",lst)
# Clearing list using slicing
lst = lst[:0]
print("List after clearing using Slicing: ",lst)
OutputList 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.
Clear a list using list comprehension method
The clear_list
function is designed to clear or empty the input list, lst
, by comprehensively filtering its elements through a list comprehension that always evaluates to False
. Here’s a simplified explanation based on your example:
- Function Definition:
clear_list(lst)
takes a list lst
as its parameter. - List Comprehension: Inside the function, a new list is created with a list comprehension
[item for item in lst if False]
. Because the condition is always False
, no elements from the original list lst
satisfy the condition, resulting in an empty list. - Return Empty List: The function returns this newly created empty list.
- Testing the Function:
input_list
is defined with elements [2, 3, 5, 6, 7]
. When clear_list
is called with input_list
, it returns an empty list []
. - Print Output: The output, which is an empty list, is printed, showing
[]
.
Python
def clear_list(lst):
lst = [item for item in lst if False]
return lst
input_list = [2, 3, 5, 6, 7]
output = clear_list(input_list)
print(output) # Output: []
Time complexity:O(n)
auxiliary space:O(n), where n is length of list.
Please Login to comment...