Menu Close

Python Dictionary pop() Method

In this dictionary method tutorial, you will learn all about the Python dictionary pop() method to remove the specified item from the dictionary.

In the previous tutorial, we have seen all about the dictionary keys method to getting all the keys from the dictionary.

To understand this example, you should have a basic knowledge of the Python dictionary.

Python Dictionary pop() Method

Python dictionary pop method is a dictionary built-in method that is used to remove the specified item. pop function in python returns the removed item.

Syntax

The syntax of dictionary pop method is:-

dictionary.pop(key, value)

Parameter

Dictionary pop function in Python accepts two parameters:-

  • key:- Required, The key name of the item you want to remove.
  • value:- Optional, A value to return if the specified key does not exist.

Return Value

Return value of pop method is value of specified key.

Python dictionary pop example:

Here we will take some example to understand dictionary pop function in Python.

Example:

student = {'first_name': 'Vishvajit', 'last_name': 'Rao', 'course': 'BCA'}
result = student.pop("course")
print(result)

Output will be:- BCA

Note:- If the value not found of the specified key, Then the pop() method raise an exception.

Conclusion

In this tutorial, you have learned all about the Python dictionary pop method to remove the specified item.
This is the best way when you want to remove any particular item from the dictionary.

If this article helped you, please keep visiting for further interesting python tutorial.

Dictionary Methods


For more information:- Click Here

Python Dictionary keys Method
Python Dictionary copy Method

Related Posts