Wednesday, December 25, 2024
Google search engine
HomeLanguagesDifferent ways to clear a list in Python

Different ways to clear a list in Python

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)
 
# Clearing list
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]
 
# 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))


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




# 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))


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]
 
# 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))


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]
 
# 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))


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




# 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)


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.

RELATED ARTICLES

Most Popular

Recent Comments