Clear a List in Python

Clear a List in Python

Rayven Esplanada Mar-15, 2021 Mar-04, 2021 Python Python List
  1. Use clear() to Clear a List in Python
  2. Set an Empty List Value to Clear a List in Python
  3. Use del and the Slice Operator : to Clear a List in Python
  4. Multiply Entire List With 0 to Clear a List in Python

This tutorial will demonstrate different ways to clear a list in Python.

Use clear() to Clear a List in Python

The easiest and most obvious way to clear a list in Python is to use the clear() method, a predefined method within Python lists.

For example, initialize a list with integer values and use the clear() function to erase all the values.

lst = [1, 6, 11, 16]
print(lst)
lst.clear()
print(lst)

Output:

[1, 6, 11, 16]
[]
<div class="panel panel-primary panel-warning">
<div class="panel-heading">Note</div>
<div class="panel-body">

This is a quick and easy solution to clear a list in Python, but this is only compatible with Python versions 3.3 and above.

Set an Empty List Value to Clear a List in Python

Another way to clear an existing list variable is to set an empty list to the variable using an empty pair of square brackets [].

lst = [1, 6, 11, 16]
print(lst)

lst = []
print(lst)

Output:

[1, 6, 11, 16]
[]

Although this also does a quick job of clearing a list, this will not remove the values of the list from memory; instead, it creates a new instance of a list and points to the newly created instance.

If no other variable is pointing to the list variable’s previous values, then those variables would be unreachable and would be up for garbage collection.

This situation can be made clear if we made two variables point to the same list and set one of those variables to an empty list afterward.

lst = [1, 6, 11, 16]
lst2 = lst
print(lst)
print(lst2)

lst = []
print(lst)
print(lst2)

Output:

[1, 6, 11, 16]
[1, 6, 11, 16]
[]
[1, 6, 11, 16]

The second list, lst2, was set to the value of lst, but even after setting the new lst value to an empty list, the list lst2 still retains the old values from before. In this case, the previous values are not eligible for garbage collection since there still is a variable pointing to it.

Use del and the Slice Operator : to Clear a List in Python

The Python keyword del is used to delete objects and their references in memory completely. The del keyword is used to delete Python objects, iterables, portions of lists, etc.

The colon operator : in Python is an operator used to slice arrays into parts that accepts two integer arguments.

The : operator is used to obtaining the actual reference of the list for deletion.

lst = [1, 6, 11, 16]
lst2 = lst
print(lst)
print(lst2)

del lst[:]
print(lst)
print(lst2)

Another solution that will do the same thing as this without using the del keyword would be to set the list as an empty list but use the : operator to replace the previous reference with the empty list.

lst = [1, 6, 11, 16]
lst2 = lst
print(lst)
print(lst2)

lst[:] = []
print(lst)
print(lst2)

The output of both solutions is as follows:

[1, 6, 11, 16]
[1, 6, 11, 16]
[]
[]

From the previous example, it is noted that setting an existing list variable to an empty list doesn’t actually delete the previous values. This solution will completely delete the previous values even if other variables point to the same reference of the values within the main list.

Multiply Entire List With 0 to Clear a List in Python

The quickest method of clearing a list would be to multiply the entire list with 0. Multiplying the list variable itself with 0 will clear the list and clear its previous reference in memory.

lst = [1, 6, 11, 16]
lst2 = lst
print(lst)
print(lst2)

lst *= 0
print(lst)
print(lst2)

Output:

[1, 6, 11, 16]
[1, 6, 11, 16]
[]
[]

This method is the quickest performing solution out of all the mentioned solutions.

In summary, the most obvious way to clear a list is to use the built-in clear() function in lists, though the quickest performing solution would be to multiply the list variable with the value 0.

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python