Python – Remove List all Items – clear() Function with Examples

clear() function is used to remove all the items of a list. This method is a part of python programming language. This method will empty the entire list.
Here is a list of some colors (red, green, blue), by using clear() method we can easily remove all the elements from the list and make it empty.

Syntax of clear() Function

The syntax of clear() function in Python is:



Parameters of clear() Function in Python

clear() method does not take any argument as a prameter.



clear() function is only available and compatible with Python 3.x. This function is one of python list methods.


Return Value of clear() Function in Python

clear() function remove all items from the list and does not return anything.


Python clear() Function Example 1

Here we have a list of some characters ('a', 'b', 'c', 'd', 'e', 'f') and we want to empty this list by using clear() method.

# a charcters list
charcters = ['a', 'b', 'c', 'd', 'e', 'f']

We get the output as empty list.


Python del() Method

clear() method is new and for older versions of python bellow 3.2, you can use del() method to achieve the same result.

# Defining a fruits list
fruit = ['apple', 'grapes', 'banana']

# clearing the list
del fruit[:]


So, we have an empty list.