Definition:
This method of dictionary is used to remove all the elements and returns an empty dictionary. Also note that this method does not delete the dictionary, it only removes its item.
Syntax:
Dictionary.clear()
This method does not take any argument, passing an argument will throw an error.
Working:
You can copy code here.
dictionary = {1:’Student’,
‘Name’:’Waqar Khan’,
‘Age’:23,
‘Male’:True,
‘Subjects’:[‘Physics’,’Maths’,’Computers’]}
dictionary.clear()
print(dictionary)
Code:
- A dictionary with variable name ‘dictionary’ is defined and some different key: value pairs are passed as data.
- clear() method of the dictionary is applied.
- Value of the variable dictionary is printed.
Output:
- An empty dictionary as output is shown.
Interview questions:
- What is the difference between clear() method of dictionary, set or lists?
clear() empty or say deletes the item from dictionary, set or lists, so technically there is no difference between them.
Related Post: