Python List clear() Method
list.clear() removes all items from a list in place, leaving the same list object empty, and returns None.Use clear() when you want to reuse a list that other parts of your program already hold a reference to, for example emptying a shopping cart after checkout or resetting a buffer between batches.
The key word is in place. The list object stays the same; only its contents go away. That is different from rebinding the name with items = [], which creates a new empty list and leaves the old one untouched. Understanding this difference is part of how Python copying and references work.
| Syntax | my_list.clear() |
|---|---|
| Parameters | None |
| Returns | None |
| Time complexity | O(n) |
How list.clear() empties a list in place
After clear() the list has length 0 and is equal to []. Any other name that points at the same list also sees it empty.
cart = ["pen", "book"]
alias = cart
cart.clear()
print(cart)
print(alias)clear() vs assigning [] vs del my_list[:]
Three ways to get an empty list behave differently when the list is shared. clear() and del my_list[:] change the existing object, while my_list = [] only moves the name to a new object. Prefer clear(): it is the most readable. It was added in Python 3.3; older code used del my_list[:].
| Code | Same list object? | Other references see empty list? |
|---|---|---|
a.clear() | Yes | Yes |
del a[:] | Yes | Yes |
a[:] = [] | Yes | Yes |
a = [] | No, a new list | No, they keep the old items |
When to use clear() and when to create a new list
If a function receives a list and is expected to empty it for the caller, use clear(). If you just want a fresh start in a local variable and nobody else shares the old list, items = [] is perfectly fine. Be careful when you have stored the list somewhere else, such as inside another list: clearing it later will also change what that outer list shows. Make a copy first if you need to keep the old contents.
Examples you can run
Press Run, then edit the code and run it again.
Empty a cart after checkout
The same list is reused for the next customer.
cart = ["pen", "book", "lamp"]
print(len(cart))
cart.clear()
print(cart)
cart.append("mug")
print(cart)Expected output3
[]
['mug']
clear() vs rebinding with shared references
b is the same list as a, so it sees the clear; c does not follow a rebinding.
a = [1, 2, 3]
b = a
a.clear()
print(b)
c = [4, 5]
d = c
c = []
print(d)Expected output[]
[4, 5]
Save a copy before clearing
A copy keeps the data safe when the original is emptied.
batch = ["a", "b"]
saved = batch.copy()
batch.clear()
print(saved)
print(batch)Expected output['a', 'b']
[]
Common mistakes
Storing the list and then clearing it
✗ Wrong
rows = []
batch = [1, 2]
rows.append(batch)
batch.clear()
print(rows)✓ Right
rows = []
batch = [1, 2]
rows.append(batch.copy())
batch.clear()
print(rows)append() stores a reference, not a copy. Append batch.copy() instead.
Using the return value
✗ Wrong
items = [1, 2]
items = items.clear()
print(items)✓ Right
items = [1, 2]
items.clear()
print(items)clear() returns None. Call it on its own line.
Calling clear without parentheses
✗ Wrong
items = [1, 2]
items.clear
print(items)✓ Right
items = [1, 2]
items.clear()
print(items)Add the parentheses to actually call the method: items.clear().
Python List clear() Method: questions people ask
How do I empty a list in Python?
Call my_list.clear(). You can also write my_list = [] to bind the name to a new empty list, but that does not change other references to the old list.
What is the difference between clear() and del list[:]?
They have the same effect: both empty the existing list in place. clear() is more readable and is the preferred form in Python 3.
Does clear() delete the list variable?
No. The variable still exists and refers to an empty list. Use del my_list to remove the name itself.
Keep learning
Step-by-step lessons:
Python ListsPython List MethodsCopying Lists and Dicts
Related:
Python List copy() MethodPython List remove() MethodPython List pop() MethodPython List Methods
More in list methods:
Python List append() MethodPython List extend() MethodPython List insert() MethodPython List remove() MethodPython List pop() MethodPython List index() MethodPython List count() MethodPython List sort() Method See all →