Python Tutoring Online
 Home

Python List clear() Method

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.

Need Help Learning Python?

Understanding Python list methods such as clear() is important when working with collections, data processing, and dynamic programs.

If you are learning Python and need help understanding list methods, programming assignments, or projects, one-to-one Python tutoring can help you develop practical programming skills.

Get Python Tutoring Help

What Does clear() Do in Python?

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.

Python
numbers = [10, 20, 30, 40]

numbers.clear()

print(numbers)

Output

[]

The list is now empty, but numbers is still a valid Python list and can be used again.

Syntax of Python list clear()

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.

Example 1: Clear a List of Numbers

The following example removes all numbers from a list.

Python
numbers = [10, 20, 30, 40]

numbers.clear()

print(numbers)

Output

[]

All four elements are removed from the list.

Example 2: Clear a List of Strings

The clear() method works with lists containing strings, as well as numbers or other Python objects.

Python
fruits = ["apple", "banana", "mango", "orange"]

fruits.clear()

print(fruits)

Output

[]

Example 3: Clear a Mixed-Data List

A list can contain different types of values, and clear() removes all of them.

Python
data = [10, "Python", 3.14, True, None]

data.clear()

print(data)

Output

[]

clear() Modifies the Existing List

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.

Python
numbers = [10, 20, 30]

other = numbers

numbers.clear()

print(numbers)
print(other)

Output

[]
[]

Both variables refer to the same list object. Clearing the list through numbers also means that other now refers to the same empty list.

clear() vs Assigning an Empty List

There is an important difference between using clear() and assigning [] to a variable.

Using clear()

Python
numbers = [10, 20, 30]

other = numbers

numbers.clear()

print(numbers)
print(other)

Output

[]
[]

The original list object is emptied, so both variables see the change.

Using Assignment

Python
numbers = [10, 20, 30]

other = numbers

numbers = []

print(numbers)
print(other)

Output

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

clear() vs del

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.

Example: clear() Compared with del

Python
numbers = [1, 2, 3]

numbers.clear()

print(numbers)


numbers = [4, 5, 6]

del numbers[:]

print(numbers)

Output

[]
[]

Both operations empty their respective lists.

Clear a List and Add New Values

A list can be cleared and then reused for new data.

Python
data = [1, 2, 3]

data.clear()

data.append(100)
data.append(200)

print(data)

Output

[100, 200]

The original values are removed before the new values are added.

Clear a List Inside a Loop

The clear() method can be useful when a temporary list is reused during repeated processing.

Python
values = []

for i in range(3):
    values.append(i)
    print("Before clearing:", values)
    values.clear()

Output

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.

Practical Example: Reset Temporary Data

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.

Python
processed = ["Record 1", "Record 2", "Record 3"]

print("Processed:", processed)

processed.clear()

print("After reset:", processed)

Output

Processed: ['Record 1', 'Record 2', 'Record 3']
After reset: []

Practical Example: Reset a Shopping Cart

An application could use clear() to empty a shopping cart after an order has been completed.

Python
cart = ["Laptop", "Mouse", "Keyboard"]

print("Cart before checkout:", cart)

cart.clear()

print("Cart after checkout:", cart)

Output

Cart before checkout: ['Laptop', 'Mouse', 'Keyboard']
Cart after checkout: []

Practical Example: Clear a List After Processing

Suppose a program collects values in a temporary list, processes them, and then needs the same list for another batch of data.

Python
batch = [10, 20, 30]

total = sum(batch)

print("Total:", total)

batch.clear()

print("Batch:", batch)

batch.extend([40, 50, 60])

print("New batch:", batch)

Output

Total: 60
Batch: []
New batch: [40, 50, 60]

Clear a Nested List

If a list contains other lists, calling clear() on the outer list removes all of its elements, including references to the inner lists.

Python
matrix = [
    [1, 2],
    [3, 4],
    [5, 6]
]

matrix.clear()

print(matrix)

Output

[]

The outer list becomes empty. The inner lists are no longer elements of matrix.

clear() Does Not Return the List

Like several Python list methods that modify a list in place, clear() returns None.

Python
numbers = [10, 20, 30]

result = numbers.clear()

print(result)
print(numbers)

Output

None
[]

The list is changed, but the method itself does not return the updated list.

Do Not Assign clear() Back to the 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()

clear() on an Already Empty List

Calling clear() on an empty list is safe. The list simply remains empty.

Python
numbers = []

numbers.clear()

print(numbers)

Output

[]

Unlike pop(), calling clear() on an empty list does not raise an exception.

clear() vs remove()

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.

clear() vs pop()

The pop() method removes one element and returns it. The clear() method removes every element and returns None.

Python
numbers = [10, 20, 30]

removed = numbers.pop()

print("Removed:", removed)
print(numbers)


numbers = [10, 20, 30]

result = numbers.clear()

print("Result:", result)
print(numbers)

Output

Removed: 30
[10, 20]
Result: None
[]

clear() vs del list[:]

Both clear() and del list[:] can empty the same existing list.

Python
numbers = [1, 2, 3]

del numbers[:]

print(numbers)


numbers = [4, 5, 6]

numbers.clear()

print(numbers)

Output

[]
[]

The clear() form is generally easier to read because it clearly communicates that the intention is to empty the list.

Important Characteristics of clear()

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

Performance of clear()

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.

When Should You Use clear()?

Use clear() when the existing list should remain available but all of its current elements need to be removed.

When Should You Use Assignment Instead?

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.

Python
numbers = [10, 20, 30]

numbers = []

print(numbers)

Output

[]

If other variables still refer to the original list, they will not be affected by this reassignment.

Common Beginner Mistakes with clear()

Mistake 1: Thinking clear() Deletes the List Variable

The clear() method removes the list's elements. It does not remove the variable itself.

Mistake 2: Expecting clear() to Return an Empty List

The method returns None. The original list becomes empty, but clear() does not return [].

Mistake 3: Assigning clear() to the Same Variable

Avoid:

numbers = numbers.clear()

Use:

numbers.clear()

Mistake 4: Confusing clear() with remove()

remove() deletes one matching value, whereas clear() removes every element from the list.

Mistake 5: Confusing clear() with pop()

pop() removes one element and returns it. clear() removes all elements and returns None.

Frequently Asked Questions About Python clear()

What is clear() in Python?

clear() is a Python list method that removes all elements from an existing list, leaving an empty list.

What is the syntax of the Python clear() method?

The syntax is:

list.clear()

It does not require any arguments.

Does clear() delete the list?

No. clear() removes all elements from the list, but the list object and variable remain available.

Does clear() return an empty list?

No. The method returns None. The original list itself is modified and becomes empty.

What is the difference between clear() and remove()?

clear() removes every element from the list. remove() removes only the first occurrence of a specified value.

What is the difference between clear() and pop()?

clear() removes all elements, while pop() removes one element and returns the removed value.

What is the difference between clear() and del?

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.

What is the difference between clear() and list = []?

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.

Can clear() be used on an empty list?

Yes. Calling clear() on an empty list is safe and leaves the list empty.

Can clear() remove only selected elements?

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.

Does clear() work with strings and numbers?

Yes. The method works with any Python list regardless of the types of objects stored in it.

Can clear() be used with nested lists?

Yes. Calling clear() on an outer list removes all of its elements, including any inner lists stored as elements.

Is clear() safe to use on an empty list?

Yes. Unlike pop(), clear() does not raise an exception when the list is already empty.

What is the time complexity of clear()?

Clearing a list is generally an O(n) operation because the existing elements must be removed from the list.

Related Python List Tutorials

Learn More Python List Methods

The clear() method is useful when you need to empty an existing list while keeping the list available for reuse. Continue learning the other Python list methods to understand how to add, remove, search, sort, reverse, copy, and manipulate list data.

Explore the complete Python list methods tutorial for syntax, examples, practical use cases, and common mistakes.

Explore Python List Methods




Stuck Learning Python?

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.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net