|
| Home |
|---|
The Python list clear() method removes all elements from
an existing list. After calling clear(), the list becomes
empty, but the list object itself continues to exist.
This makes clear() useful when a program needs to reset an
existing list, discard temporary data, or reuse the same list object
for a new set of values.
The clear() method removes every element from the list.
It does not remove the list variable or destroy the list object.
Instead, the existing list is modified so that it contains zero
elements.
numbers = [10, 20, 30, 40]
numbers.clear()
print(numbers)
[]
The list is now empty, but numbers is still a valid Python
list and can be used again.
The syntax of the Python list clear() method is:
list.clear()
The method does not require any arguments or parameters. It simply removes all elements from the list on which it is called.
The following example removes all numbers from a list.
numbers = [10, 20, 30, 40]
numbers.clear()
print(numbers)
[]
All four elements are removed from the list.
The clear() method works with lists containing strings,
as well as numbers or other Python objects.
fruits = ["apple", "banana", "mango", "orange"]
fruits.clear()
print(fruits)
[]
A list can contain different types of values, and
clear() removes all of them.
data = [10, "Python", 3.14, True, None]
data.clear()
print(data)
[]
One important characteristic of clear() is that it
modifies the existing list rather than creating a new empty list.
This distinction becomes important when multiple variables refer to
the same list.
numbers = [10, 20, 30]
other = numbers
numbers.clear()
print(numbers)
print(other)
[]
[]
Both variables refer to the same list object. Clearing the list through
numbers also means that other now refers to
the same empty list.
There is an important difference between using clear()
and assigning [] to a variable.
numbers = [10, 20, 30]
other = numbers
numbers.clear()
print(numbers)
print(other)
[]
[]
The original list object is emptied, so both variables see the change.
numbers = [10, 20, 30]
other = numbers
numbers = []
print(numbers)
print(other)
[]
[10, 20, 30]
Here, numbers = [] makes numbers refer to a
new empty list. The original list still contains its previous values,
so other continues to show them.
The del statement can also remove all elements from a
list by deleting the complete list slice.
| Approach | Purpose |
|---|---|
list.clear() |
Removes all elements from the existing list |
del list[:] |
Deletes all elements from the existing list |
list = [] |
Makes the variable refer to a new empty list |
For readability, clear() is usually the most direct way
to express the intention of emptying an existing list.
numbers = [1, 2, 3]
numbers.clear()
print(numbers)
numbers = [4, 5, 6]
del numbers[:]
print(numbers)
[]
[]
Both operations empty their respective lists.
A list can be cleared and then reused for new data.
data = [1, 2, 3]
data.clear()
data.append(100)
data.append(200)
print(data)
[100, 200]
The original values are removed before the new values are added.
The clear() method can be useful when a temporary list is
reused during repeated processing.
values = []
for i in range(3):
values.append(i)
print("Before clearing:", values)
values.clear()
Before clearing: [0]
Before clearing: [1]
Before clearing: [2]
The same list is reused during each iteration. Its contents are cleared before the next iteration begins.
Programs often use lists to temporarily store values while processing a group of records. Once processing is complete, the list can be cleared before it is reused.
processed = ["Record 1", "Record 2", "Record 3"]
print("Processed:", processed)
processed.clear()
print("After reset:", processed)
Processed: ['Record 1', 'Record 2', 'Record 3']
After reset: []
An application could use clear() to empty a shopping
cart after an order has been completed.
cart = ["Laptop", "Mouse", "Keyboard"]
print("Cart before checkout:", cart)
cart.clear()
print("Cart after checkout:", cart)
Cart before checkout: ['Laptop', 'Mouse', 'Keyboard']
Cart after checkout: []
Suppose a program collects values in a temporary list, processes them, and then needs the same list for another batch of data.
batch = [10, 20, 30]
total = sum(batch)
print("Total:", total)
batch.clear()
print("Batch:", batch)
batch.extend([40, 50, 60])
print("New batch:", batch)
Total: 60
Batch: []
New batch: [40, 50, 60]
If a list contains other lists, calling clear() on the
outer list removes all of its elements, including references to the
inner lists.
matrix = [
[1, 2],
[3, 4],
[5, 6]
]
matrix.clear()
print(matrix)
[]
The outer list becomes empty. The inner lists are no longer elements
of matrix.
Like several Python list methods that modify a list in place,
clear() returns None.
numbers = [10, 20, 30]
result = numbers.clear()
print(result)
print(numbers)
None
[]
The list is changed, but the method itself does not return the updated list.
Because clear() returns None, you should not
write:
numbers = numbers.clear()
After this statement, numbers would contain
None instead of referring to the list.
Call the method directly:
numbers.clear()
Calling clear() on an empty list is safe. The list simply
remains empty.
numbers = []
numbers.clear()
print(numbers)
[]
Unlike pop(), calling clear() on an empty
list does not raise an exception.
The clear() and remove() methods both modify
lists, but they have very different purposes.
| Feature | clear() |
remove() |
|---|---|---|
| Removes | All elements | First matching value |
| Requires an argument? | No | Yes |
| List becomes empty? | Yes | Not necessarily |
| Return value | None |
None |
Use clear() when the entire list needs to be emptied.
Use remove() when only a particular value needs to be
deleted.
The pop() method removes one element and returns it.
The clear() method removes every element and returns
None.
numbers = [10, 20, 30]
removed = numbers.pop()
print("Removed:", removed)
print(numbers)
numbers = [10, 20, 30]
result = numbers.clear()
print("Result:", result)
print(numbers)
Removed: 30
[10, 20]
Result: None
[]
Both clear() and del list[:] can empty the
same existing list.
numbers = [1, 2, 3]
del numbers[:]
print(numbers)
numbers = [4, 5, 6]
numbers.clear()
print(numbers)
[]
[]
The clear() form is generally easier to read because it
clearly communicates that the intention is to empty the list.
| Characteristic | Description |
|---|---|
| Purpose | Removes all elements from a list |
| Syntax | list.clear() |
| Arguments | None |
| Modifies original list | Yes |
| Return value | None |
| Result | List contains zero elements |
| Works on an empty list | Yes |
| Supports different element types | Yes |
The clear() method removes all elements from the list.
Because every existing element has to be removed from the list's
contents, the operation is generally O(n) with
respect to the number of elements.
For ordinary Python programs, clear() is the straightforward
choice when an existing list needs to be emptied.
Use clear() when the existing list should remain available
but all of its current elements need to be removed.
Use assignment such as numbers = [] when you specifically
want the variable to refer to a new empty list and you do not need
other references to the original list to see the change.
numbers = [10, 20, 30]
numbers = []
print(numbers)
[]
If other variables still refer to the original list, they will not be affected by this reassignment.
The clear() method removes the list's elements. It does
not remove the variable itself.
The method returns None. The original list becomes empty,
but clear() does not return [].
Avoid:
numbers = numbers.clear()
Use:
numbers.clear()
remove() deletes one matching value, whereas
clear() removes every element from the list.
pop() removes one element and returns it.
clear() removes all elements and returns
None.
clear() is a Python list method that removes all elements
from an existing list, leaving an empty list.
The syntax is:
list.clear()
It does not require any arguments.
No. clear() removes all elements from the list, but the
list object and variable remain available.
No. The method returns None. The original list itself is
modified and becomes empty.
clear() removes every element from the list.
remove() removes only the first occurrence of a specified
value.
clear() removes all elements, while pop()
removes one element and returns the removed value.
clear() is a list method specifically designed to remove
all elements. del list[:] can also empty an existing list,
while del can additionally delete individual indexes,
ranges, or the variable itself.
clear() empties the existing list object. Assignment to
[] makes the variable refer to a new empty list.
This difference matters when multiple variables reference the same
list.
Yes. Calling clear() on an empty list is safe and leaves
the list empty.
No. clear() removes all elements. To remove a particular
value, use remove(). To remove an element by index, use
pop() or del, depending on the requirement.
Yes. The method works with any Python list regardless of the types of objects stored in it.
Yes. Calling clear() on an outer list removes all of its
elements, including any inner lists stored as elements.
Yes. Unlike pop(), clear() does not raise an
exception when the list is already empty.
Clearing a list is generally an O(n) operation because the existing elements must be removed from the list.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net