How to Remove All the Elements from a List in Python?

There are several methods for removing all the elements from a Python list. Some of the ways used to accomplish this are listed below:

Remove All the Elements from a List in Python

Method #1: Using clear() Function

clear() is a built-in method in the Python list. This method allows us to remove all of the elements from the list as well as their references. This method does not accept any arguments and returns no value.

Syntax:

list.clear()

Example

Approach:

  • Give the list as static input and store it in a variable
  • Print the given list
  • Apply clear() function on the given list to remove all the elements of the given list
  • Print the given list after clearing it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable
gvn_lst = [10, 11, 12, 13]
# Print the given list
print("The given list = ", gvn_lst)
# Apply clear() function on the given list to remove all the 
# elements of the given list
gvn_lst.clear()
# Print the given list after clearing it.
print("The given list after clearing:", gvn_lst)

Output:

The given list = [10, 11, 12, 13]
The given list after clearing: [ ]

Method #2: Using del statement

You can use the del() function to remove items at a specific index or to remove all the elements, making the list empty.

The del statement is commonly used in Python to delete objects. Because everything in Python is an object, this statement can delete a part of the list via list slicing. This approach has an impact on the other references in the list.

Syntax:

del list[:]

list[:] – indicates the list elements from starting to end.

Example

Approach:

  • Give the list as static input and store it in a variable
  • Print the given list.
  • Remove all the elements of the given list using the del statement.
  • Print the given list after clearing it.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable
gvn_lst = [10, 11, 12, 13]
# Print the given list
print("The given list = ", gvn_lst)
# Remove all the elements of the given list using the del statement
del gvn_lst[:]
# Print the given list after clearing it.
print("The given list after clearing:", gvn_lst)

Output:

The given list = [10, 11, 12, 13]
The given list after clearing: [ ]

You can also remove the element as the specific index using the del statement as shown below:

# Give the list as static input and store it in a variable
gvn_lst = [10, "hello", 12, 13]
# Print the given list
print("The given list = ", gvn_lst)
# Remove the element at the 1st index of the given list using the del statement
del gvn_lst[1]
# Print the given list after removing the element at the given index.
print("The given list after removing the element at the 1st index:")
print(gvn_lst)

Output:

The given list = [10, 'hello', 12, 13]
The given list after removing the element at the 1st index:
[10, 12, 13]

Method #3: Using list Re-Initialization

We can re-initialize a list by simply assigning it an empty list. Here, we take a list and then assign an empty list to it, resulting in an empty list.

Syntax:

gvn_lst = [ ]

Approach:

  • Give the list as static input and store it in a variable
  • Print the given list.
  • Re-initialize the given list.
  • Print the given list after Re-initializing.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable
gvn_lst = [10, "hello", 12, 13]
# Print the given list
print("The given list = ", gvn_lst)
# Re-initialize the given list
gvn_lst = []
# Print the given list after Re-initializing.
print("The given list after Re-initializing =", gvn_lst)

Output:

The given list = [10, 'hello', 12, 13]
The given list after Re-initializing = [ ]

Method #4: By multiplying the list with 0

In this method, we simply assign 0 to all elements in the list, resulting in an empty list. The character * represents all elements.

# Give the list as static input and store it in a variable
gvn_lst = [10, "hello", 12, 13]
# Print the given list
print("The given list = ", gvn_lst)
# Assign 0 to all the elements of the given list using *= which empties
# given list
gvn_lst *= 0
# Print the given list after removing all the elements
print("The given list after removing all the elements:", gvn_lst)

Output:

The given list = [10, 'hello', 12, 13]
The given list after removing all the elements: [ ]