{"id":3761,"date":"2023-10-24T16:19:48","date_gmt":"2023-10-24T10:49:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=3761"},"modified":"2023-11-10T11:57:22","modified_gmt":"2023-11-10T06:27:22","slug":"python-dictionary-pop-function-and-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-dictionary-pop-function-and-examples\/","title":{"rendered":"Python Dictionary: Pop() Function and Examples"},"content":{"rendered":"
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.<\/p>\n
The list of key value pairs in curly braces that is separated by comma defines a dictionary. Column \u2018:\u2019 separates the value of each key.<\/p>\n
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\u2014probably a list of tuples.<\/p>\n
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.<\/p>\n
Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n Return:<\/strong><\/p>\n 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 \u2018this’. We can implement this as below.<\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 \u2018:\u2019 separates the value …<\/p>\ndictionary.pop(key,default)<\/code><\/p>\n
key: The key for which the key-value pair must be returned and removed.\r\nDefault value :If the specified key is not present, the default value is returned.<\/pre>\n
If key is present, returns the value associated with the deleted key-value pair.\r\nIf no key is specified, the default value is used.\r\nIf the key is not present and the default value is not specified, a KeyError is returned.<\/pre>\n
2)Removing a key-value pair from dictionary using pop()<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# given key which should be removed\r\ngivenkey = 'this'\r\n# printing the dictionary before modification\r\nprint(\"Before Modification\", dictionary)\r\n# removing 'this' key from dictionary\r\n# storing its value in value function\r\nkeyvalue = dictionary.pop(givenkey)\r\n# printing the value\r\nprint(\"Value of key :\", keyvalue)\r\n# printing the dictionary after modification\r\nprint(\"After Modification\", dictionary)\r\n<\/pre>\n
Before Modification {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\nValue of key : 200\r\nAfter Modification {'is': 100, 'BTechGeeks': 300}<\/pre>\n
3)Removing the key which doesn’t exist in given dictionary(with default value)<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# given key which should be removed\r\ngivenkey = 'hello'\r\n# printing the dictionary before modification\r\nprint(\"Before Modification\", dictionary)\r\n# removing 'this' key from dictionary\r\n# storing its value in value function\r\nkeyvalue = dictionary.pop(givenkey, 500)\r\n# printing the value\r\nprint(\"Value of key :\", keyvalue)\r\n# printing the dictionary after modification\r\nprint(\"After Modification\", dictionary)\r\n<\/pre>\n
Before Modification {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\nValue of key : 500\r\nAfter Modification {'this': 200, 'is': 100, 'BTechGeeks': 300}<\/pre>\n
4)Removing the key which doesn’t exist in given dictionary(without default value)<\/h3>\n
# given dictionary\r\ndictionary = {'this': 200, 'is': 100, 'BTechGeeks': 300}\r\n# given key which should be removed\r\ngivenkey = 'hello'\r\n# printing the dictionary before modification\r\nprint(\"Before Modification\", dictionary)\r\n# removing 'this' key from dictionary\r\n# storing its value in value function\r\nkeyvalue = dictionary.pop(givenkey)\r\n# printing the value\r\nprint(\"Value of key :\", keyvalue)\r\n# printing the dictionary after modification\r\nprint(\"After Modification\", dictionary)\r\n<\/pre>\n
Traceback (most recent call last):\r\n File \"\/home\/bbdc799e66bc794f9534237294f5b64c.py\", line 9, in <module>\r\n keyvalue = dictionary.pop(givenkey)\r\nKeyError: 'hello'<\/pre>\n
\n