Python List clear() method [With Examples]

In this Python tutorial, we will discuss the Python List clear() method. It is an essential method of the list class that allows us to remove all the items from a Python list.

List clear() method in Python

Below are the topics that we are doing to discuss in this article:

  • Introduction to Python List clear() method
  • Syntax of the clear() method
  • Purpose and use cases of the clear() method

Python List clear() method

The clear() method is a built-in Python list method that removes all the items from a Python list, leaving it empty. This is particularly useful when we want to reuse a Python list without maintaining the previously stored elements.

The syntax for using the Python clear() method is as follows:

list_name.clear()

Here, ‘list_name’ refers to the name of the list we want to clear.

clear() method in Python List Examples

Let’s take a look at examples of how to use the clear() method:

Example#1 Basic Usage of the clear() Method

Consider a Python list of popular tourist destinations in the United States. After visiting all the locations on the Python list, we want to clear the list to create a new one for our next trip.

tourist_destinations = ['Grand Canyon', 'Yellowstone National Park', 'Statue of Liberty', 'Mount Rushmore']

print("Original list of tourist destinations:", tourist_destinations)

tourist_destinations.clear()

print("List after clear():", tourist_destinations)

In this example, we have a Python list called ‘tourist_destinations’ containing four popular US tourist destinations. We first print the original list, and then use the Python List clear() method to remove all the elements from the list.

After the clear() method is executed, the list becomes empty, as demonstrated by the second print statement.

Output:

List clear method in Python
List clear method in Python

Example#2 Resetting a list to its initial state for reuse in a program

Imagine we are working with a Python list of polling data from various states in the US during an election year. We want to analyze the data state by state, and after each analysis, we want to clear the list to make room for the next state’s data.

poll_data_california = [45, 55, 60, 40, 50]
poll_data_texas = [30, 70, 65, 35, 40]

# Analyzing California poll data
print("California poll data:", poll_data_california)
# Perform analysis
poll_data_california.clear()

# Now poll_data_california is empty and ready for the next state's data
print("California poll data after clear():", poll_data_california)

# Analyzing Texas poll data
print("Texas poll data:", poll_data_texas)
# Perform analysis
poll_data_texas.clear()

# Now poll_data_texas is empty and ready for the next state's data
print("Texas poll data after clear():", poll_data_texas)
  • First, we create two Python lists containing poll data for California and Texas.
  • We print and analyze California’s poll data. After the analysis, we use the Python clear() method to empty the ‘poll_data_california’ list.
  • Next, we print and analyze Texas’s poll data. After the analysis, we use the clear() method to empty the ‘poll_data_texas’ Python list.

Output:

Python List clear method
Python List clear method

Example#3 Freeing up memory by removing unused elements in a list

Suppose we have an online store where users can add items to their shopping carts. When a user completes a purchase, we would want to clear their cart to allow them to start a new shopping session.

shopping_cart = ['Laptop', 'Headphones', 'Mouse']

print("User's shopping cart:", shopping_cart)
# Complete purchase process
shopping_cart.clear()

print("User's shopping cart after clear():", shopping_cart)
  • We create a list called ‘shopping_cart’ containing three items (Laptop, Headphones, and Mouse).
  • We print the user’s current shopping cart.
  • After simulating the purchase process, we use the clear() method to empty the ‘shopping_cart’ Python list.
  • We print the user’s shopping cart after the clear() method has been executed to demonstrate that the cart is now empty.

Output:

Python List clear method example
Python List clear method example

Example#4 Ensuring that a list is empty before appending new elements

We are collecting weather data for different cities in the US. We have a list to store the daily temperature readings for each city, and at the end of each day, we want to clear the list to store the readings for the next day.

daily_temps_new_york = [72, 74, 75, 73, 71]

print("New York daily temperatures:", daily_temps_new_york)
# Perform data analysis
daily_temps_new_york.clear()

print("New York daily temperatures after clear():", daily_temps_new_york)
  • We create a list called ‘daily_temps_new_york’ containing five temperature readings.
  • We print the current daily temperatures for New York.
  • After performing data analysis, we use the clear() method to empty the ‘daily_temps_new_york’ Python list.
  • We print the daily temperatures for New York after the clear() method has been executed to demonstrate that the Python list is now empty.

Output:

List clear method in Python example
List clear method in Python example

Conclusion

The Python list clear() method is an efficient and convenient way to empty a list, making it a valuable tool in your programming toolkit. With its simple syntax and fast operation, the clear() method can be applied in various programming scenarios.

You may also like to read the following articles: