Dictionaries are the implementation by Python of a data structure associative array. A dictionary is a collection of pairs of key values. A key pair and its associated value represent each key pair.
The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column ‘:’ separates the value of each key.
A dictionary cannot be sorted only to get a representation of the sorted dictionary. Inherently, dictionaries are orderless, but not other types, including lists and tuples. Therefore, you need an ordered data type, which is a list—probably a list of tuples.
Pop() was defined in the Python language for almost all containers, including list, set, and so on. This article will demonstrate the pop() method provided by Python dictionaries. This method is useful for programmers who frequently use the dictionary.
Pop Function in Python Dictionary with Examples
- pop() function
- Removing a key-value pair from dictionary using pop()
- Removing the key which doesn’t exist in given dictionary(with default value)
- Removing the key which doesn’t exist in given dictionary(without default value)
1)pop() function
Syntax:
dictionary.pop(key,default)
Parameters:
key: The key for which the key-value pair must be returned and removed. Default value :If the specified key is not present, the default value is returned.
Return:
If key is present, returns the value associated with the deleted key-value pair. If no key is specified, the default value is used. If the key is not present and the default value is not specified, a KeyError is returned.
2)Removing a key-value pair from dictionary using pop()
Assume we have a dictionary with keys of strings and values of integers. Now we want to remove an entry from the dictionary with the key ‘this’. We can implement this as below.
# given dictionary dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300} # given key which should be removed givenkey = 'this' # printing the dictionary before modification print("Before Modification", dictionary) # removing 'this' key from dictionary # storing its value in value function keyvalue = dictionary.pop(givenkey) # printing the value print("Value of key :", keyvalue) # printing the dictionary after modification print("After Modification", dictionary)
Output:
Before Modification {'this': 200, 'is': 100, 'BTechGeeks': 300} Value of key : 200 After Modification {'is': 100, 'BTechGeeks': 300}
3)Removing the key which doesn’t exist in given dictionary(with default value)
If we attempt to remove a key from the dictionary that does not exist. The default value will then be returned by the pop() function.
Below is the implementation:
# given dictionary dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300} # given key which should be removed givenkey = 'hello' # printing the dictionary before modification print("Before Modification", dictionary) # removing 'this' key from dictionary # storing its value in value function keyvalue = dictionary.pop(givenkey, 500) # printing the value print("Value of key :", keyvalue) # printing the dictionary after modification print("After Modification", dictionary)
Output:
Before Modification {'this': 200, 'is': 100, 'BTechGeeks': 300} Value of key : 500 After Modification {'this': 200, 'is': 100, 'BTechGeeks': 300}
4)Removing the key which doesn’t exist in given dictionary(without default value)
When we use the pop() function, we try to remove a key from the dictionary that does not exist, and we do not pass the default value. The pop() function will then throw a KeyError.
Below is the implementation:
# given dictionary dictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300} # given key which should be removed givenkey = 'hello' # printing the dictionary before modification print("Before Modification", dictionary) # removing 'this' key from dictionary # storing its value in value function keyvalue = dictionary.pop(givenkey) # printing the value print("Value of key :", keyvalue) # printing the dictionary after modification print("After Modification", dictionary)
Output:
Traceback (most recent call last): File "/home/bbdc799e66bc794f9534237294f5b64c.py", line 9, in <module> keyvalue = dictionary.pop(givenkey) KeyError: 'hello'
Related Programs:
- python dictionary pop function and examples
- python filter function tutorial and examples
- python how to unpack list tuple or dictionary to function arguments using and
- python map function with examples
- python dictionary get function tutorial examples
- python open a file using open with statement and benefits explained with examples
- python numpy ravel function tutorial with examples