In this tutorial, you’ll learn four different ways to use Python to clear a list. You’ll learn how to use the clear
method, the del
statement, the *=
operator, and list item re-assignment. Knowing how to clear a list is a task that should seem easy, but is an often elusive skill. After reading this tutorial, you’ll have learned four different ways to clear a Python list in order to start fresh.
The Quick Answer: Use clear
Table of Contents
What are Python Lists?
Lists in Python are one of the main data structures that exist in the language. They have some unique attributes, similar to arrays in other languages. They can hold values of different data types, meaning that they’re heterogeneous. They are also ordered, meaning that you can access the items by their index position. Python list indices start at 0 and increment by 1. Python lists can also contain duplicate values.
In the next section, you’ll learn how to clear a Python list with the .clear()
method.
Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!
Clear a Python List with Clear
One of the easiest ways to clear a Python list is to the use the list.clear()
method. The method empties a given list and doesn’t return any items. The method was introduced in Python 3.2, so if you’re using an earlier version, you’ll need to read on to learn how to use the del
operator.
Let’s see how we can empty a list by using the clear
method:
# Clear a Python List with .clear()
items = ['datagy', 1, 2, 3]
items.clear()
print(items)
# Returns: []
Let’s break down what we did above:
- We instantiated a list
- We then applied the
clear
method to empty the list - We print the list to ensure that the list is emptied
Now that we know how to use the clear
method, we can explore some additional properties about it. The method doesn’t accept any arguments and it modifies a list in place (meaning that we don’t re-assign it to another list).
In the next section, you’ll learn how to empty a Python list with the del
keyword.
Want to learn how to pretty print a JSON file using Python? Learn three different methods to accomplish this using this in-depth tutorial here.
Clear a Python List with del
The Python del
keyword allows us to delete a particular list item or a range of list items at particular indices. If you want to learn how to use the Python del
keyword to delete list items, check out my in-depth tutorial here. Since we can use the Python del
keyword to delete items or ranges of items in a list, we can simply delete all items in the list using the :
operator.
Let’s see how we can use the del
keyword to empty a Python list:
# Clear a Python List with del
items = ['datagy', 1, 2, 3]
del items[:]
print(items)
# Returns: []
Let’s break down what we did here:
- We instantiated a Python list,
items
- We then used the
del
keyword to delete items from the first to the last index - We then printed the list to make sure it was empty
Interestingly, the .clear()
method works exactly the same as the del [:]
keyword, under the hood. It really just represents syntactical sugar in order to make the process a little easier to understand.
In the next section, you’ll learn how to empty a Python list using the *=
operator.
Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!
Clear a Python List with the *= Operator
Python allows us to chain operators together. For example, writing a = a + 1
is the same as writing a += 1
. Another way to chain operators is to use the *=
, which reassigns the variable multiplied by zero. When we apply this operator a list, we assign the value of 0 to each item, thereby removing the items from the list.
Let’s see how we can use the *=
operator to empty a Python list of all of its elements:
# Clear a Python List with *=
items = ['datagy', 1, 2, 3]
items *= 0
print(items)
# Returns: []
In the next section, you’ll learn how to use Python to empty a list using list item re-assignment.
Want to learn how to use the Python zip()
function to iterate over two lists? This tutorial teaches you exactly what the zip()
function does and shows you some creative ways to use the function.
Clear a Python List with List Item Re-assignment
While it may seem intuitive to empty a list simply by writing some_list = []
. This approach doesn’t actually delete the items from the list, it just removes the reference to the items. If the list has also been assigned to another variable, it’ll be retained in memory.
We can, however, assign the full range of items to be empty items. This will delete all of the items in a list, thereby clearing it.
Let’s see how we can do this with Python:
# Clear a Python List with *=
items = ['datagy', 1, 2, 3]
items[:] = []
print(items)
# Returns: []
In the example above, we can see that we re-assigned all of the list items by using the list indexing method. We were able to access all of the items and assign them an empty value.
Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.
Conclusion
In this tutorial, you learned four different ways to use Python to clear a list. You learned how to use the .clear()
method, the del
keyword, the *=
operator, and list item re-assignment. Being able to work with lists in different ways is an important skill for a Pythonista of any level.
To learn more about Python lists, check out the official documentation here.