{"id":3483,"date":"2022-12-01T19:00:10","date_gmt":"2022-12-01T19:00:10","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3483"},"modified":"2022-12-01T19:00:12","modified_gmt":"2022-12-01T19:00:12","slug":"python-dictionary-popitem","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-dictionary-popitem\/","title":{"rendered":"Python Dictionary popitem() Method With Example"},"content":{"rendered":"\n<p class=\"has-text-align-center\">Python Dictionary popitem<\/p>\n\n\n\n<p>Now, we are going to learn about the popitem method. Whenever we are required to remove the last added key-value pair from the dictionary, we can do that using the popitem method. So, we can simply say that the popitem method removes the last inserted key-value pair from the dictionary.<\/p>\n\n\n\n<p>Remember, if the dictionary is empty, and you call the popitem method over the dictionary, then it raises a KeyError.<\/p>\n\n\n\n<p>Let\u2019s now have a look at a program, for a better understanding of the popitem method &#8211;<\/p>\n\n\n\n<h2 class=\"has-text-align-center wp-block-heading\">Python Dictionary popitem() Method<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"219\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-popitem-METHOD-1.jpg\" alt=\"\" class=\"wp-image-3486\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-popitem-METHOD-1.jpg 648w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-popitem-METHOD-1-300x101.jpg 300w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/figure>\n<\/div>\n\n\n<p>In the above program, you can see that we have a sample dictionary (you can consider an example of your own, but here we are understanding the method, so we have created a sample dictionary)<\/p>\n\n\n\n<p>In the dictionary, you can see that the element with the key &#8216;four&#8217; is the last inserted element. So, according to the definition of the method, the last inserted element would get removed.<\/p>\n\n\n\n<p>We also printed the dictionary before and after calling the method.<\/p>\n\n\n\n<p>Let\u2019s now have a look at the output &#8211;<\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">C:\\Users\\GyaniPandit\\Desktop\\python&gt; python dictionary.py<br>{&#8216;one&#8217;: 1, &#8216;two&#8217;: 2, &#8216;three&#8217;: 3, &#8216;four&#8217;: 4}<br>{&#8216;one&#8217;: 1, &#8216;two&#8217;: 2, &#8216;three&#8217;: 3}<br>(&#8216;four&#8217;, 4)<\/p>\n\n\n\n<p>As you can see, the element with key &#8216;four&#8217; is last inserted into the dictionary, according to the program and then we call the popitem method, and the last inserted element is removed.<\/p>\n\n\n\n<p>The popitem method returns the removed element as a tuple, as can be seen in the output.<\/p>\n\n\n\n<p>So, if you ever need to remove the last inserted key-value pair, you can make use of the popitem method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Dictionary popitem Now, we are going to learn about the popitem method. Whenever we are required to remove the last added key-value pair from the dictionary, we can do that using the popitem method. So, we can simply say that the popitem method removes the last inserted key-value pair from the dictionary. Remember, if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3483","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3483"}],"version-history":[{"count":3,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3483\/revisions"}],"predecessor-version":[{"id":4155,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3483\/revisions\/4155"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}