The clear()
method in Python empties a list or dictionary.
The syntax of the clear()
method is as follows.
objectName.clear()
This method neither takes any parameters nor does it return a value. clear
simply removes all the elements from a list or dictionary.
In the code below, we use the clear()
method to empty the vowels
list and contents
dictionary.
# listvowels = ['a','e','i','o','u']# empty the listvowels.clear()# print the empty listprint('Empty Vowel list:', vowels)# dictionarycontents = {'A': 1, 'B': 2, 'C': 3}# empty the dictionarycontents.clear()# print the empty listprint('Empty Contents Dictionary:', contents)