Python List clear() Method
list.clear() removes every item from a list.
Use it when you want to empty an existing list in place. This is useful when you want to keep using the same list object, but remove all of its contents.
Quick answer
items = [1, 2, 3]
items.clear()
print(items) # []
Use clear() to remove all items from an existing list in place.
What list.clear() does
list.clear():
- Removes every item from a list
- Changes the original list in place
- Leaves you with an empty list:
[] - Takes no arguments
This means the list still exists after calling clear(), but it contains no values.
Basic syntax
Syntax:
my_list.clear()
Important points:
- Call it on a list object
- Do not pass values inside the parentheses
- Include the parentheses so the method actually runs
Simple example
Here is a basic example:
numbers = [10, 20, 30, 40]
numbers.clear()
print(numbers)
Output:
[]
What happens here:
numbersstarts with four itemsnumbers.clear()removes all of them- Printing the list shows that it is now empty
If you are new to lists, see Python lists explained for beginners.
Return value
clear() returns None.
It does not create a new list.
items = ["a", "b", "c"]
result = items.clear()
print(items) # []
print(result) # None
This is a common beginner mistake. If you write:
items = [1, 2, 3]
items = items.clear()
print(items)
Then items becomes None, not an empty list.
clear() vs assigning []
These two lines may look similar, but they are not the same:
my_list.clear()
my_list = []
clear() empties the same list object
a = [1, 2, 3]
b = a
a.clear()
print(a) # []
print(b) # []
Both a and b point to the same list, so clearing it affects both variables.
my_list = [] creates a new empty list
a = [1, 2, 3]
b = a
a = []
print(a) # []
print(b) # [1, 2, 3]
Here, a now points to a new empty list. But b still points to the original list.
This difference matters when:
- Two variables refer to the same list
- A list is shared between parts of a program
- You want to empty the original list instead of replacing it
This idea is also helpful when learning the list copy() method.
When to use clear()
Use clear() when you want to:
- Reset a list before reusing it
- Empty shared list data in place
- Remove all items without deleting the list variable
Example:
shopping_cart = ["milk", "bread", "eggs"]
# Order completed, reuse the same list
shopping_cart.clear()
print(shopping_cart) # []
If you only want to remove one item, see how to remove an item from a list in Python or the list.remove() method. If you want to remove and return one item, see the list.pop() method.
Common beginner mistakes
Writing my_list.clear instead of my_list.clear()
This does not call the method.
items = [1, 2, 3]
items.clear
print(items)
Output:
[1, 2, 3]
Nothing changes because the method was not run.
Correct version:
items = [1, 2, 3]
items.clear()
print(items) # []
Expecting clear() to return the emptied list
clear() returns None, not the list.
items = [1, 2, 3]
result = items.clear()
print(result) # None
print(items) # []
Using clear() on a value that is not a list
Some types have a clear() method, but not all of them.
This will fail:
text = "hello"
text.clear()
A string does not have a clear() method.
You may also get errors if your variable is None or another unexpected type. If that happens, print the type first.
Useful checks:
print(my_list)
print(type(my_list))
print(id(my_list))
other = my_list
my_list.clear()
print(other)
These can help you see:
- What the variable currently contains
- Whether it is really a list
- Whether two variables point to the same object
If you run into method-related problems, you may also need help with fixing attribute errors in Python.
FAQ
Does list.clear() delete the list?
No. It keeps the list object but removes all items from it.
Does list.clear() return a new list?
No. It returns None and changes the existing list in place.
What is the difference between clear() and my_list = []?
clear() empties the same list object. my_list = [] points the variable to a new empty list.
Can I use clear() on other types?
Some types like dictionaries and sets also have clear(), but strings and tuples do not.