What is the clear() method in Python?

The clear() method in Python empties a list or dictionary.

Syntax

The syntax of the clear() method is as follows.

objectName.clear()

Parameters and return value

This method neither takes any parameters nor does it return a value. clear simply removes all the elements from a list or dictionary.

Code

In the code below, we use the clear() method to empty the vowels list and contents dictionary.

# list
vowels = ['a','e','i','o','u']
# empty the list
vowels.clear()
# print the empty list
print('Empty Vowel list:', vowels)
# dictionary
contents = {'A': 1, 'B': 2, 'C': 3}
# empty the dictionary
contents.clear()
# print the empty list
print('Empty Contents Dictionary:', contents)

Free Resources