Python List clear() Method


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.

Syntax of clear() 🔝

my_list.clear()

The clear() method accepts no arguments.

Removing All Items from a List 🔝

my_list=['Alex', 'Ronald', 'John', 'Ravi']

my_list.clear()

print(my_list)
Output
[]

All elements are removed, but my_list remains a valid list.

Return Value of clear() 🔝

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
[]

Do Not Assign clear() Back to the List

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.

Checking the List Length After clear() 🔝

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
[]

clear() and del with List Slicing 🔝

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.

Difference Between clear() and del my_list 🔝

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.

Another Reference Can Still Exist

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.

clear() vs Assigning an Empty 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.

Using clear()

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.

Assigning []

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.

Using clear() with Nested Lists 🔝

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.

Clearing a List Inside a Function 🔝

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.

Clearing and Reusing a 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.

Summary of clear() 🔝

  • clear() removes all items from an existing list.
  • It takes no arguments.
  • The original list remains available after being cleared.
  • The list length becomes 0.
  • The method modifies the list in place.
  • The return value of 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.
  • Assigning [] creates a new empty list for that variable rather than clearing the existing shared list object.
  • Calling clear() through one reference affects other variables that refer to the same list.
All List Methods pop() remove() del Questions with Solutions on List




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer