The Python clear() method removes all items from an existing
list. After the method runs, the list still exists but contains no elements.
The clear() method changes the original list in place and does not require any arguments.
my_list.clear()
The clear() method accepts no arguments.
my_list=['Alex', 'Ronald', 'John', 'Ravi']
my_list.clear()
print(my_list)
Output
[]
All elements are removed, but my_list remains a valid list.
The clear() method modifies the existing list and returns None.
my_list=[1, 2, 3]
result=my_list.clear()
print(result)
print(my_list)
Output
None
[]
Because clear() returns None, assigning its result back to the list variable replaces that variable with None.
my_list=[1, 2, 3]
my_list=my_list.clear()
print(my_list)
Output
None
Call clear() directly when you want to empty an existing list.
The len() function returns 0 after all items have been removed.
my_list=['Alex', 'Ronald', 'John']
print("Before clear:", len(my_list))
my_list.clear()
print("After clear:", len(my_list))
print(my_list)
Output
Before clear: 3
After clear: 0
[]
The del keyword can also remove all elements while keeping the list itself available.
my_list=['Alex', 'Ronald', 'John']
del my_list[:]
print(my_list)
print(type(my_list))
Output
[]
<class 'list'>
The expression del my_list[:] removes every item selected by the full
list slice. The list remains available and its type is still list.
For simply removing all items, clear() expresses the intention more directly.
There is an important difference between clearing a list and deleting the variable that refers to it.
With clear(), the variable continues to refer to an empty list.
my_list=[1, 2, 3]
my_list.clear()
print(my_list)
Output
[]
Using del my_list removes the name my_list from the current scope.
my_list=[1, 2, 3]
del my_list
print(my_list)
Sample Error
NameError: name 'my_list' is not defined
The error occurs because the variable name my_list is no longer defined.
This does not necessarily mean that the underlying list object is immediately removed. If another variable refers to the same list, that reference can still be used.
my_list=[1, 2, 3]
another_list=my_list
del my_list
print(another_list)
Output
[1, 2, 3]
The list is still accessible through another_list.
Both of the following can make my_list appear empty:
my_list.clear()
and
my_list=[]
However, they behave differently when another variable refers to the same list.
my_list=[1, 2, 3]
another_list=my_list
my_list.clear()
print(my_list)
print(another_list)
Output
[]
[]
Both variables refer to the same list object. Clearing that list is therefore visible through both variables.
my_list=[1, 2, 3]
another_list=my_list
my_list=[]
print(my_list)
print(another_list)
Output
[]
[1, 2, 3]
Here, my_list=[] makes my_list refer to a new empty list. The variable another_list still refers to the original list containing the three numbers.
This distinction is useful when the same list is shared between different parts of a program.
A list can contain other lists. Calling clear() on one inner list removes only the elements from that inner list.
my_list=[
[1, 2],
[3, 4],
[5, 6]
]
my_list[0].clear()
print(my_list)
Output
[[], [3, 4], [5, 6]]
Only the first inner list is cleared. The other rows remain unchanged. Read more about 2D lists.
Because lists are mutable, a function can clear a list that is passed to it.
def reset_list(my_list):
my_list.clear()
data=[1, 2, 3]
reset_list(data)
print(data)
Output
[]
The function receives a reference to the list and clear() modifies that existing list.
After clearing a list, we can continue using the same list and add new items.
my_list=['old data', 'more data']
my_list.clear()
my_list.append('Alex')
my_list.append('Ravi')
print(my_list)
Output
['Alex', 'Ravi']
The append() method can add new items after the previous contents have been removed.
clear() removes all items from an existing list.0.clear() is None.del my_list[:] can also remove all items while retaining the list.del my_list removes the variable name from the current scope.[] creates a new empty list for that variable rather than clearing the existing shared list object.clear() through one reference affects other variables that refer to the same list.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.