In this article, we’ll learn what Python dictionary pop method is, its role in Python dictionaries and how to properly use it.
Multiple Python code examples will be provided with detailed with step by step explanation to better understand how Python dictionary pop method works.
After reading this post, you’ll have an in-depth knowledge of how to use pop method practically.
What is Python Dictionary Pop Method?
This method is used to remove/delete items from Python dictionary. Let’s now practically understand it with proper examples. But first, let’s understand its syntax.
Syntax of Pop Method
dictionaryName.pop( key, value(optional) )
We have to pass a specific key to this method. Pair of key and value will be removed of that specific key from dictionary.
If you use only key and the key is found then it will delete this key value pair. But if not found, then it’ll show a key error exception.
So for this reason, a default value can be passed as the second argument of pop method.
Implementing Python Dictionary Pop Method (Multiple Examples)
Below examples will practically demonstrate the role of pop method.
Example 1: Remove Item From Dictionary
dictValues={'name':'Zeeshan','age':24,'height':6}
print(dictValues) // {'name': 'Zeeshan', 'age': 24, 'height': 6}
dictValues.pop('age') // it will remove the key value pair of that specific key
print(dictValues) // {'name': 'Zeeshan', 'height': 6}
Output
{'name': 'Zeeshan', 'age': 24, 'height': 6}
{'name': 'Zeeshan', 'height': 6}
Pop method has successfully removed the key value pair of the key passed to it as an argument.
Example 2: Key Not Found
Let’s now see what will happen if the key passed to pop method is not found in dictionary. See below code:
dictValues={'name':'Zeeshan','age':24,'height':6} dictValues.pop('weight') // KeyError: 'weight'
Output
KeyError: 'weight'
In that case, we’ve to give our Python dictionary pop method a default value. See below code:
dictValues={'name':'Zeeshan','age':24,'height':6} print(dictValues.pop('weight',70))
Output
70
You can see that if the key is not found in dictionary, then the default value will be used. In our case, it is 70. You can pass values of other datatypes as well like list, set, tuple, string, float or even a dictionary.
Example 3: Return Value from Pop Method
dictValues={'name':'Zeeshan','age':24,'height':6} print(dictValues.pop('name'))
Output
Zeeshan
As you can see, it returns the value of the key passed to it as an argument.
You can also pass the returned value to a variable. See below code:
dictValues={'name':'Zeeshan','age':[24,32,21,29,15],'height':6} val=dictValues.pop('age') print(val)
Output
[24, 32, 21, 29, 15]
So this is how you can easily use Python dictionary pop method. Hope you like this post.
Feel free to ask if you still have questions regarding the implementation of Python dictionary pop method. I’ll be very glad to answer all.
Conclusion
To conclude, now you have an in-depth practical understanding of how to properly use Python dictionary pop method. I’d be very happy to receive your valuable feedback on this post. Thank you for reading it.