{"id":3367,"date":"2023-10-21T09:52:22","date_gmt":"2023-10-21T04:22:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=3367"},"modified":"2023-11-10T11:52:40","modified_gmt":"2023-11-10T06:22:40","slug":"python-programming-dictionary-methods","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/","title":{"rendered":"Python Programming \u2013 Dictionary Methods"},"content":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Dictionary Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Python Programming \u2013 Dictionary Methods<\/h2>\n

Dictionary methods<\/strong><\/p>\n

The following are some dictionary methods supported by Python.<\/p>\n

diet . clear ( )\r\nRemoves all items from the dictionary.\r\n\r\n>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }\r\n>>> len ( a )\r\n2 \r\n>>> a . clear ( )\r\n>>> len ( a )\r\n0<\/pre>\n

diet. copy ( )
\nReturns a copy of the dictionary.<\/p>\n

>>> dict1 = { ' Name ' : ' Zara ' , ' Age ' : 7 ]\r\n>>> dict2=dict1 . copy ( )\r\n>>> dict2\r\n{ ' Age ' : 7 , ' Name ' : ' Zara ' }<\/pre>\n

diet. fromkeys ( seq [ , value ] )
\nCreate a new dictionary with keys from seq and values set to value (default as None).<\/p>\n

>>> seq= ( ' name ' , ' age ' , ' gender ' )\r\n>>> a=dict . fromkeys ( seq )\r\n>>> a \r\n{ ' gender ' : None , ' age ' : None , ' name ' : None }\r\n>>> a=a.fromkeys ( seq , 10 )\r\n>>> a\r\n{ ' gender ' : 10 , ' age ' : 10 , ' name ' : 10 }<\/pre>\n

diet . get ( key [ , default ] )
\nReturn the value for the key, if the key is in the dictionary, else default. If the default is not given, the default is None.<\/p>\n

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }\r\n>>> a.get ( ' Age ' )\r\n7\r\n>>> a. get ( ' Gender ' , ' Never ' )\r\n' Never '\r\n>>> print a.get ( ' Gender ' )\r\nNone<\/pre>\n

diet.has_key ( key )
\nTest for the presence of a key in the dictionary. Returns True, if the key is present in the dictionary, otherwise False.<\/p>\n

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }\r\n>>> a.has_key ( ' Age ' )\r\nTrue\r\n>>> a.has_key ( ' Gender ' )\r\nFalse<\/pre>\n

diet.items ( )
\nReturns a list of dictionary’s key-value tuple pairs.<\/p>\n

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 } \r\n>>> a . items ( )\r\n[ ( ' age ' , 7 ) , ( ' Name ' , ' Zara ' ) ]<\/pre>\n

diet.iteritems ( )
\nReturns an iterator over the dictionary’s key-value pairs.<\/p>\n

>>> a= { ' Gender ' : ' Female ' , ' Age 1 : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' } \r\n>>> for b in a . iteritems ( ) :\r\n. . . print ' { 0 } - - - -{ 1 } ' . format ( b [ 0 ] , b [ 1 ] )\r\n. . .\r\nGender - - - -Female \r\nAge - - - - 7 \r\nHair color- - - - None1\r\nName- - - - Zara<\/pre>\n

diet.iterkeys ( )
\nReturns an iterator over the dictionary’s keys.<\/p>\n

>>> a= { 1 Gender 1 : \u2019 Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }\r\n>>> for b in a.iterkeys ( ) :\r\n. . . print b\r\n. . . \r\nGender\r\nAge \r\nHair color \r\nName<\/pre>\n

diet.itervalues ( )
\nReturns an iterator over the dictionary’s values.<\/p>\n

>>> a= { 1 Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }\r\n>>> for b in a.itervalues ( ) :\r\n. . . print b\r\n. . .\r\nFemale\r\n7\r\nNone\r\nZara<\/pre>\n

diet.keys ( )
\nReturns list of dictionary keys.<\/p>\n

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }\r\n>>> a . keys ( )\r\n[ ' Age ' , ' Name ' ]<\/pre>\n

diet.pop ( key [ , default ] )
\nIf key is.in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.<\/p>\n

>>> a= { ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }\r\n>>> a . pop ( ' Age ' , 15 )\r\n7\r\n>>> a . pop ( ' Age ' , 15 )\r\n15 \r\n>>> a\r\n{ ' Gender ' : ' Female ' , ' Hair color ' : None , ' Name ' : ' Zara ' }<\/pre>\n

diet. popitem ( )
\nRemove and return an arbitrary key-value pair from the dictionary. If the dictionary is empty, calling popitem () raises an KeyError exception.<\/p>\n

>>> a= { ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' } \r\n>>> a . popitem ( )\r\n( ' Gender ' , ' Female ' )\r\n>>> a . popitem ( )\r\n( ' Age ' , 7 )\r\n>>> a\r\n{ ' Hair color ' : None , ' Name ' : ' Zara ' }<\/pre>\n

diet . setdefault ( key [ , default ] )
\nIf key is in the dictionary, return its value. If not, insert key with a value of default and return default. The default defaults to None.<\/p>\n

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }\r\n>>> a . setdefault ( ' Age ' , 15 )\r\n7\r\n>>> a . setdefault ( ' Gender ' , ' Female ' )\r\n' Female '\r\n>>> a . setdefault ( ' Hair color ' )\r\n>>> a\r\n{ ' Gender ' : ' Female ' , ' Age ' : 7 , ' Hair color ' : None , ' Name ' : ' Zara ' }<\/pre>\n

diet.update ( [ other ] )
\nUpdate the dictionary with the key-value pairs from other, overwriting existing keys, and returns None. The method accepts either another dictionary object or an iterable of key-value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key-value pairs.<\/p>\n

>>> dict1= { ' Name ' : ' Zara ' , ' Age ' : 7 }\r\n>>> dict2= { ' Gender ' : ' female ' }\r\n>>> dict1 . update ( dict2 )\r\n>>> dict1 . update ( hair_color= ' black ' , eye_color= ' blue ' )\r\n>>> dict1\r\n{ ' eye_color ' : ' blue ' , ' Gender ' : ' female ' , ' Age ' : 7 , ' Name ' : ' Zara ' , ' hair_color ' : ' black ' }<\/pre>\n

diet . values ( )
\nReturn list of dictionary values.<\/p>\n

>>> a= { ' Name ' : ' Zara ' , ' Age ' : 7 }\r\n>>> a . values ( )\r\n[ 7, ' Zara ' ]<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Dictionary Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Dictionary Methods Dictionary methods The following are some dictionary methods supported by Python. diet . clear ( ) Removes all items from the dictionary. >>> a= { ‘ Name …<\/p>\n

Python Programming \u2013 Dictionary Methods<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Dictionary Methods - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Programming \u2013 Dictionary Methods - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Python Programming \u2013 Dictionary Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Dictionary Methods Dictionary methods The following are some dictionary methods supported by Python. diet . clear ( ) Removes all items from the dictionary. >>> a= { ' Name … Python Programming \u2013 Dictionary Methods Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-21T04:22:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:22:40+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/\",\"name\":\"Python Programming \u2013 Dictionary Methods - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-21T04:22:22+00:00\",\"dateModified\":\"2023-11-10T06:22:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming \u2013 Dictionary Methods\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Programming \u2013 Dictionary Methods\",\"datePublished\":\"2023-10-21T04:22:22+00:00\",\"dateModified\":\"2023-11-10T06:22:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#webpage\"},\"wordCount\":318,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Programming \u2013 Dictionary Methods - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/","og_locale":"en_US","og_type":"article","og_title":"Python Programming \u2013 Dictionary Methods - Python Programs","og_description":"In this Page, We are Providing Python Programming \u2013 Dictionary Methods. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Dictionary Methods Dictionary methods The following are some dictionary methods supported by Python. diet . clear ( ) Removes all items from the dictionary. >>> a= { ' Name … Python Programming \u2013 Dictionary Methods Read More »","og_url":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-21T04:22:22+00:00","article_modified_time":"2023-11-10T06:22:40+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#webpage","url":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/","name":"Python Programming \u2013 Dictionary Methods - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-21T04:22:22+00:00","dateModified":"2023-11-10T06:22:40+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-programming-dictionary-methods\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Programming \u2013 Dictionary Methods"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Programming \u2013 Dictionary Methods","datePublished":"2023-10-21T04:22:22+00:00","dateModified":"2023-11-10T06:22:40+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-programming-dictionary-methods\/#webpage"},"wordCount":318,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-programming-dictionary-methods\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3367"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3367"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3367\/revisions"}],"predecessor-version":[{"id":3368,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3367\/revisions\/3368"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}