{"id":24057,"date":"2021-10-14T09:13:04","date_gmt":"2021-10-14T03:43:04","guid":{"rendered":"https:\/\/python-programs.com\/?p=24057"},"modified":"2021-11-05T17:14:08","modified_gmt":"2021-11-05T11:44:08","slug":"python-program-for-dictionary-popitem-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-dictionary-popitem-method\/","title":{"rendered":"Python Program for Dictionary popitem() Method"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Dictionary keys() Function<\/a> A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element\u2019s keys.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dictionary popitem() Function:<\/strong><\/p>\n Python popitem() is a Dictionary function that removes the most recently inserted key-value pair and prints the removed pair.<\/p>\n Syntax:<\/strong><\/p>\n Parameter Values:<\/strong><\/p>\n The dictionary popitem() function doesn’t have any parameters.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Learn several Python Dictionary Method Examples<\/a> on our page and access the dictionary items easily.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program for Dictionary keys() Function Dictionary in python : A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element\u2019s keys. Examples: Example1: Input: Given dictionary = {‘hello’: 120, ‘this’: 130, ‘is’: 140, ‘btechgeeks’: 150} Output: The …<\/p>\n
\nDictionary in python :<\/strong><\/p>\nGiven dictionary = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}<\/pre>\n
The above given dictionary is :\r\n{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}\r\nThe key-value pair removed from the given dictionary= ('btechgeeks', 150)\r\nThe above given dictionary after the removal of the last item(key-value):\r\n{'hello': 120, 'this': 130, 'is': 140}<\/pre>\n
Given dictionary = {10: 'good', 60: 'morning', 140: 'btechgeeks'}<\/pre>\n
The above given dictionary is :\r\n{10: 'good', 60: 'morning', 140: 'btechgeeks'}\r\nThe key-value pair removed from the given dictionary= (140, 'btechgeeks')\r\nThe above given dictionary after the removal of the last item(key-value):\r\n{10: 'good', 60: 'morning'}<\/pre>\n
Program for Dictionary popitem() Method in python<\/h2>\n
\n
dictionary.popitem()<\/pre>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the dictionary as static input by initializing it with some random values\r\n# and store it in a variable.\r\ngvn_dictinry = {'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}\r\n# Print the above-given dictionary.\r\nprint(\"The above given dictionary is :\")\r\nprint(gvn_dictinry)\r\n# Apply dictionary.popitem() method to the above-given dictionary to remove the last\r\n# inserted key value pair\r\n# Store it in another variable.\r\nremvd_lstitem = gvn_dictinry.popitem()\r\n# Print the above removed key-value pair.\r\nprint(\"The key-value pair removed from the given dictionary=\", remvd_lstitem)\r\n# Print the above-given dictionary after removal of last item.\r\nprint(\"The above given dictionary after the removal of the last item(key-value):\")\r\nprint(gvn_dictinry)\r\n<\/pre>\n
The above given dictionary is :\r\n{'hello': 120, 'this': 130, 'is': 140, 'btechgeeks': 150}\r\nThe key-value pair removed from the given dictionary= ('btechgeeks', 150)\r\nThe above given dictionary after the removal of the last item(key-value):\r\n{'hello': 120, 'this': 130, 'is': 140}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dictinry = {}\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dictinry[keyy] = valuee\r\n\r\n# Print the above-given dictionary.\r\nprint(\"The above given dictionary is :\")\r\nprint(gvn_dictinry)\r\n# Apply dictionary.popitem() method to the above-given dictionary to remove the last\r\n# inserted key value pair\r\n# Store it in another variable.\r\nremvd_lstitem = gvn_dictinry.popitem()\r\n# Print the above removed key-value pair.\r\nprint(\"The key-value pair removed from the given dictionary=\", remvd_lstitem)\r\n# Print the above-given dictionary after removal of last item.\r\nprint(\"The above given dictionary after the removal of the last item(key-value):\")\r\nprint(gvn_dictinry)\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 3\r\nEnter key and value separated by spaces = 10 good\r\nEnter key and value separated by spaces = 60 morning\r\nEnter key and value separated by spaces = 140 btechgeeks\r\nThe above given dictionary is :\r\n{'10': 'good', '60': 'morning', '140': 'btechgeeks'}\r\nThe key-value pair removed from the given dictionary= ('140', 'btechgeeks')\r\nThe above given dictionary after the removal of the last item(key-value):\r\n{'10': 'good', '60': 'morning'}<\/pre>\n
\n