clear() removes every item, leaving an empty list []. The list object itself survives, so the change is in place.
Every other name, dict, or function argument that refers to the same list sees it empty too. That is the difference from lst = [], which only points one name at a brand-new list. del lst[:] does exactly what clear() does.
list.clear() is an instance method of the built-in list type: call it on a list with dot notation, such as thread.clear(...). No import is needed.
Syntax
Call it with no arguments; there is nothing useful to keep from the result.
python
lst.clear()
Call
What it does
[1, 2, 3].clear()
Returns None; the list becomes [].
del lst[:]
Same effect as clear(): deletes every item of the same list.
lst = []
Not the same: rebinds the name to a new list, and other references keep the old items.
clear() was added in Python 3.3, which is why older code often uses del lst[:]. del lst is different again: it deletes the name, not the items.
Method signatures
python
list.clear()
# returns: None; the list is now empty
Parameters
clear() takes no parameters.
Return Value
Return Value
Description
None
Always None. The same list object is now empty, with length 0.
Python Examples
Example 1 — Clear One Chat Thread in an Inbox
The thread is stored inside a dict and also bound to a variable. Clearing through the variable empties the stored list.
Rebinding leaves data behind.cart = [] points the name at a new list, so the session still shows shoes and socks.
clear() reaches the real list. After pointing cart back at the stored list, clear() empties the object the session holds.
del with a full slice.del cart[:] removes all three new items from that same list, exactly like clear().
Empty and equal. The session shows an empty cart, and cart == [] is True.
Example 3 — Flush a Sensor Buffer Every Three Readings
A helper saves a full batch and then clears the caller's list, so collection restarts from empty.
python
def flush(batch):
print("saving", batch, "total", sum(batch))
batch.clear()
buffer = []
for reading in [4, 8, 15, 16, 23, 42, 7]:
buffer.append(reading)
if len(buffer) == 3:
flush(buffer)
print("left over:", buffer)
Output
saving [4, 8, 15] total 27
saving [16, 23, 42] total 81
left over: [7]
How It Works
Fill to three. Each reading is appended, and when the buffer holds three values it is handed to flush().
Totals.sum() gives 4 + 8 + 15 = 27 for the first batch and 16 + 23 + 42 = 81 for the second.
Clearing the caller's list.batch is the same object as buffer, so batch.clear() empties it for the loop as well. Writing batch = [] would only change the local name.
Partial batch. The seventh reading never reached three items, so it waits in the buffer.
Frequently Asked Questions
None. It empties the list in place, so there is nothing to assign.
clear() empties the existing list, so every reference sees it empty. lst = [] only makes the name lst point to a new list; other references keep the old items.
Yes, both delete every item from the same list object. del lst, without the slice, deletes the variable name instead.
No. The list still exists and can be reused; it simply has no items and a length of 0.
Call clear() on the parameter, as in batch.clear(). Assigning batch = [] inside the function only rebinds the local name and leaves the caller's list unchanged.