In this Python tutorial, we’ll learn about Python dictionary clear method. We’ll first understand role of this method in dictionary. After that, we’ll learn how to use it by implementing it in practical Python code examples.
Introduction: Python Dictionary Clear Method
It’s a method of Python dictionary. As the name suggests, this method is used to remove all items from a dictionary.
Let’s first understand the syntax of clear method. Then we’ll see how to properly use it in practical Python programs.
Syntax of clear() Method
dictionary.clear()
- This method does not take any arguments.
- Applying clear method on an empty dictionary does not result in raising an exception. It just works perfectly fine.
- Nothing is returned by this method. It means this method just remove all the items from the specified dictionary.
Implementing Python Dictionary Clear Method (Multiple Examples)
In order to completely understand the use of clear method, we’ve to follow the below Python programs.
Example 1: clear() applied on Dictionary
dictValues = {'name':'Zeeshan','age':25,'id':3573839,'email':'zeerockyf5@gmail.com'} dictValues.clear() print(dictValues)
Output
{}
- In this code example, we’ve first created a Python dictionary and have give it some key value pairs.
- Then we’ve applied clear method on it.
- To see the results visually, we’ve printed the dictionary using Python print function. It shows an empty dictionary which means all the items(key/value pairs) has been removed.
Example 2: clear() Applied on an Empty Dictionary
dictVals = {} dictVals.clear() print(dictVals)
Output
{}
- In this example, we’ve created an empty dictionary.
- Then we’ve applied clear method on it and displayed it.
- We can see that applying Python dictionary clear method on an empty dictionary does not raise any exception. It execute without any errors.
Example 3: Return Value of Python Dictionary Clear Method
dictValues = {'name':'Yasir','age':26,'id':8588} retValue = dictValues.clear() print(retValue)
Output
None
- First we’ve created a simple Python dictionary.
- Then we’ve applied clear method on it.
- We also have stored the value returned by this method inside a variable.
- For demonstration, we’ve displayed value of this variable using print function of Python and it shows None. It means this method does not return anything. It just remove all the items from specified dictionary.
Example 4: Clear List Of Dictionaries Using Python While Loop
lstDict=[{'a':5,'b':203},{'name':'Usman'},{'color':'green','number':4563}] i=0 while i<len(lstDict): lstDict[i].clear() i+=1 print(lstDict)
Output
[{}, {}, {}]
- In this code, we’ve created a Python list of dictionaries.
- Also, we’ve created an integer with value o. Its been used to execute the while loop until its value reaches the length of list. For that, we’ve incremented value of this integer by 1 in every iteration.
- Inside the body of while loop, we’ve applied clear method on items(dictionaries) of list by using the index value of items.
- Finally, we’ve printed the list and it shows empty dictionary items. So in this way, we got our desired output.
Example 5: clear Python List of Dictionaries using Python For Loop
listDict=[{'aa':54,'bb':23},{'name':'Hasan'},{'color':'blue','no.':4747}] for item in listDict: item.clear() print(listDict)
Output
[{}, {}, {}]
- In this program, we’ve created a list of Python dictionaries.
- Then we’ve specified a Python for loop which will take each item(dictionary) from list while iterating over it. Click here to learn more about iterating over multiple Python lists simultaneously.
- In the body of for loop, we’ve applied clear method on the items(dictionaries) of list.
- In order to see the result, we’ve displayed the list using print function. It shows a list of empty dictionaries and that’s our desired output.
So this is how we can use Python dictionary clear method. Do feel free to share it with other Python developers if you’ve learned something useful from it.
Also, feel free to ask if you still have queries related to Python dictionary clear method. We’ll be very delighted to solve all.
Conclusion
To conclude, hope you now have an in-depth understanding of how to use Python dictionary clear method properly. Do feel free to share your valuable thoughts with us in the comment section.
Other worth reading posts on Python programming are also posted on this site. Feel free to pay a visit to them as well. Thank you for reading this article.