Python List clear Function

The Python list clear() function is used to delete or remove all items/elements from a list. It completely deletes the existing items and keeps the list object empty.

Python List clear() syntax

The syntax of the list clear() function is

list_name.clear()

Parameters: The clear() method does not accept any arguments.

Return Value: The clear() method does not return any value; it simply empties the given list. In short, it modifies the original list by deleting all the existing items.

Python list clear() function Example

In this example, we declared an integer list. Next, we used a clear() function on the numeric list to remove all elements from it.

a = [10, 20, 30, 40]

print("Before : ", a)
a.clear()
print("After  : ", a)
Before :  [10, 20, 30, 40]
After  :  []

NOTE: The clear() function deletes all the existing items in a list (erases the content). However, the list object remains the same.

List of strings example

In the following example, we declared a list of strings (fruits) and used the clear() function to remove all elements.

Fruits = ['Apple', 'Banana', 'Kiwi', 'Grape']

print("Before : ", Fruits)
Fruits.clear()
print("After : ", Fruits)
Before :  ['Apple', 'Banana', 'Kiwi', 'Grape']
After  :  []

The Python list clear() method returns None

By now, we all understand the clear() function removes all the existing list items. After executing the clear() method, printing the original list returns an empty list. However, when you assign a result to a new list, it returns None as the output.

To demonstrate the clear() function return value, we declared a string list and a list of positive and negative numbers.

Fruits = ['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry']
numbers = [9, 4, -5, 0, 22, -1, 2, 14]

print(Fruits)
print(numbers)

New_Fruits = Fruits.clear()
print("\nNew: ", New_Fruits)

new_numbers = numbers.clear()
print("New Number: ", new_numbers)
['Apple', 'Orange', 'Banana', 'Kiwi', 'Grape', 'Blackberry']
[9, 4, -5, 0, 22, -1, 2, 14]

New:  None
New Number:  None

TIP: Please refer to the List and List functions article in Python.

Using Python list clear() method on User-inputs

In this program, we are allowing the user to enter the length of a list. Next, we used a for Loop to append those numbers. Then we used this method to delete the items.

intClearList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intClearList.append(value)
    
print("Before Clear() - Items in this List are : ", intClearList)
intClearList.clear()
print("After Clear()  - Items in this List are : ", intClearList)
Python List clear() function example

It allows users to enter their own string or words, and then it removes those items. You can also use it on Mixed and Nested.

str1 = []
 
number = int(input("Please enter the Total Number of Elements: "))
for i in range(1, number + 1):
    value = input("%d Element : " %i)
    str1.append(value)
    
print("Before = ", st1)
str1.clear()
print("After  = ", str1)
Please enter the Total Number of Elements: 4
1 Element : Dragon
2 Element : Cherry
3 Element : Kiwi
4 Element : Banana
Before  =  ['Dragon', 'Cherry', 'Kiwi', 'Banana']
After   =  []

Using the clear() method on an empty list

In the following example, we declared an empty list and used the Python list clear() function on it. As the original list is empty, the clear() method displays the same empty list. If you assign it to a new list, it returns None as the output.

n = []
res = n.clear()
print(res)
print(n)
None
[]

Empty the Mixed list example

In the following example, we use the built-in clear function for a list of mixed items, including integers, strings, floating-point numbers, Boolean values, and characters. The clear() function does not consider the data type of the items in the list; it simply removes all the items from it.

n = [10, "Hi", True, 20.5, 'N', 40]
n.clear()
print(n)
[]

Using Python clear() function on Nested List

In the following example, we use the clear() function to delete all elements/items in a given nested list. As you can see, the clear() method deletes all the sub-lists (nested lists) within the original list and returns an empty list.

n = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
n.clear()
print(n)
[]

clear() and append()

We can combine the Python list clear() method with the remaining list functions to perform different operations. For instance, using the clear() function to delete existing items and using insert(), extend(), or append() to add a new item.

a = [10, 20, 30, 40]
a.clear()
print(a)
a.append(100)
print(a)
[]
[100]

Python list clear() function alternatives

Reassigning to an empty list []

Another way of clearing the list items is by reassigning the original list to an empty list ([]). It is the most straightforward approach.

In the example below, we reassigned the list a to an empty list ([]). When you print the variable, it returns an empty list.

a = [10, 15, 20, 25, 30]
a = []
print(a)
[]

Empty the list using the del keyword

The del keyword is useful to delete a portion of the list. However, if we use it without the start and end index positions, it deletes all the existing list items.

a = [10, 20, 30, 40]
del a[:]
print(a)
[]

Using the pop() method and loops

The while loop iterates over the list items, and the pop() method removes the last item from it. It deletes one item at a time, and by the completion of the while loop iteration, the list becomes empty.

a = [10, 20, 30, 40, 50, 60]

while a:
    a.pop()

print(a)
[]

Using the multiplication assignment operator

To delete all the list items, we can also use the multiplication assignment operator (*=).

a = [10, 20, 30, 40, 50, 60]

a *= 0
print(a)
[]

Python list clear() vs assigning an empty list

In the previous sections, we have explained both the clear() method and reassigning the original to an empty list. Here, we will focus on the differences.

The built-in clear() function modifies the original list in place without creating a new list. It is very helpful because the list object remains the same, and the content is removed. So, we can reuse it.

In the following example, we declared an integer list and assigned it to a new variable. When you apply the clear() method to the original list, it deletes the items in the original list and also the reference list.

a = [1, 2, 3]
b = a
print(a)
print(b)
a.clear()
print(a)
print(b)
[1, 2, 3]
[1, 2, 3]
[]
[]

When you reassign the original list to an empty list, the variable is simply pointing to a new list. However, the old list items won’t be deleted if there are any references to them.

Here, b referencing the original list a. a = [] means a is reassigned to a new list, but the list items [5, 10, 15, 20] still exist because they are referred by b.

a = [5, 10, 15, 20]
b = a
print(a)
print(b)
a = []
print(a)
print(b)
[5, 10, 15, 20]
[5, 10, 15, 20]
[]
[5, 10, 15, 20]