Signup/Sign In

Python Program to clear a list

In this tutorial, we will learn how to clear a list in Python. Clearing a list simply means deleting all the elements of that list. There are many methods to clear a list in Python, we will be discussing all those methods in this tutorial.

Input: [3, 8, 1, 4, 9]

Output: [ ]

Approach To Clear a List in Python

In Python, there are 4 ways for clearing a list:

  1. using del() method
  2. using clear() method
  3. by re-initializing the list
  4. using *=0 expression

We will look at each approach in detail.

Approach 1: Clear by using del

In this approach, we will use the del() method, del() is used to clear all the list elements within a range. If we don’t give a range, all the elements in the list will be deleted.

Algorithm

Follow the algorithm to understand the approach better:

Step 1- Initialise a list

Step 2- Print the existing list

Step 3- Delete all list elements by giving [:] as range

Step 4- Print list after deletion

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

#clearing a list
#using del()

list = ['Studytonight', 1, 2, 3]
print("Existing list: ",list)

# Removing all elements
del list[:]
print("After deleting all elements: ")
print(list)


Existing list: ['Studytonight', 1, 2, 3]
After deleting all elements:
[]

Approach 2: clear() method

In this approach, we will use the clear() method. It is a predefined function in the Python library that completely empties the list.

Algorithm

Follow the algorithm to understand the approach better

Step 1- Initialise a list

Step 2- Print the existing list

Step 3- Delete the list using list.clear() function

Step 4- Print list after deletion

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach

#clearing a list
#using clear()

list = ['Studytonight', 1, 2, 'list']
print("Existing list: ",list)

# Removing all elements
list.clear()
print("After deleting all elements: ")
print(list)


Existing list: ['Studytonight', 1, 2, 'list']
After deleting all elements:
[]

Approach 3: Clear by using re-initialise list

In this approach, we will re-initialize the list by assigning an empty list to it.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise a list

Step 2- Print the existing list

Step 3- Re-initialise an empty list to the original list

Step 4- Print list after deletion

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach.

#clearing a list
#re initialise
list = ['Study','tonight', 1, 2]
print("Existing list",list)
# Removing all elements
list = []
print("After deleting all elements")
print(list)


Existing list ['Study', 'tonight', 1, 2]
After deleting all elements
[]

Approach 4: using *=0

In this approach, we just assign 0 to all elements in the list which makes the list empty. The * is a character representing all elements. This is a lesser-known method for clearing a list.

Syntax- list_name *= 0

Algorithm

Follow the algorithm to understand the approach better

Step 1- Initialise a list

Step 2- Print the existing list

Step 3- Delete list by assigning 0 to all elements

Step 4- Print list after deletion

Python Program 4

Look at the program to understand the implementation of the above-mentioned approach.

#clearing a list
#using *0
list = ['Study','tonight', 1, 2, 3]
print("Existing list",list)
# Removing all elements
list *= 0
print("After deleting all elements")
print(list)


Existing list ['Study', 'tonight', 1, 2, 3]
After deleting all elements
[]

Conclusion

In this tutorial, we have discussed all the different methods for clearing a list. All the approaches are efficient for clearing a list.