What is clear() method in dictionary | How to use the clear() method of a dictionary.

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:

Clear()_method_dictionary_python_code

clear()_method_python_output

You can copy code here.

dictionary = {1:’Student’,
‘Name’:’Waqar Khan’,
‘Age’:23,
‘Male’:True,
‘Subjects’:[‘Physics’,’Maths’,’Computers’]}

dictionary.clear()
print(dictionary)

Code:

  1. A dictionary with variable name ‘dictionary’ is defined and some different key: value pairs are passed as data.
  2. clear() method of the dictionary is applied.
  3. Value of the variable dictionary is printed.

Output:

  1. 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:

Waqar Khan

Hello, everyone, I am Waqar Khan. I have done my B.tech in Computer Science. I love to write about Computer Informational blogs. Before write I did proper research on it. Hope you like our blogs. Thank you.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *