Remove all elements from a Python List

This tutorial will discuss multiple ways to remove all elements from a Python list.

Table Of Contents

Method 1: Using the clear() method

To remove all the elements from a list, the list class in Python provides the clear() method. It can delete all the elements from the list. For example, suppose we have a list of integers like this. Now, if we want to remove all the elements from the list, we can call the clear() method on this list object. Doing so should delete all the elements from the list.

Let’s see the complete example,

sampleList = [11, 22, 33, 44, 55]
print("Original List:", sampleList)

# Clearing the list using clear method
sampleList.clear()

print("List after removing all elements:", sampleList)

Output

Original List: [11, 22, 33, 44, 55]
List after removing all elements: []

Method 2: Using Slicing

We can also use the slicing technique in Python to remove all the elements from a list. Select all the elements of the list using this operator and assign an empty list to it. This action will essentially clear out all the elements from the list, making it empty.

Let’s see the complete example,

sampleList = [11, 22, 33, 44, 55]
print("Original List:", sampleList)

# Clearing the list using clear method
sampleList[:] = []
print("List after removing all elements:", sampleList)

Output

Original List: [11, 22, 33, 44, 55]
List after removing all elements: []

Summary

Today, we learned about remove all elements from a Python list.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top