{"id":3374,"date":"2023-10-21T10:36:28","date_gmt":"2023-10-21T05:06:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=3374"},"modified":"2023-11-10T11:52:32","modified_gmt":"2023-11-10T06:22:32","slug":"python-programming-dictionary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-dictionary\/","title":{"rendered":"Python Programming \u2013 Dictionary"},"content":{"rendered":"

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

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

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

Another useful mutable built-in type is “dictionary”. A dictionary is an unordered group of comma-separated “key: value” pairs enclosed within braces, with the requirement that the keys are unique within a dictionary. The main operation of a dictionary is storing a value corresponding to a given key and extracting the value for that given key. Unlike sequences, which are indexed by a range of numbers, the dictionary is indexed by key (key should be of an immutable type, strings and numbers can always be keys).<\/p>\n

Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. The list cannot be used as keys, since lists are of mutable type. Also, as mentioned previously, Python allows adding a trailing comma after the last item of the dictionary.<\/p>\n

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a\r\n{ ' sape ' : 4139 , ' jack ' : 4098 , ' guido ' : 4127 }\r\n>>> a [ ' jack ' ]\r\n4098\r\n>>> a= { ' sape ' : 4139, ' guido ' : 4127, ' jack ' : 4098 , }\r\n>>> a\r\n{ ' sape ' : 4139 , ' jack ' : 4098 , ' guido ' : 4127 }<\/pre>\n

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

Dictionary can be created in many ways.<\/p>\n

Using curly braces<\/strong><\/p>\n

Placing a comma-separated record of key-value pairs within the braces adds initial key-value pairs to the dictionary; this is also the way dictionaries are written as output.<\/p>\n

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a \r\n{ ' sape ' , : 4139 , ' jack ' : 4098 , ' guido ' : 412 7 }\r\n\r\nA pair of braces creates an empty dictionary.\r\n\r\n>>> a= { }\r\n>>> a \r\n{ }\r\n>>> type ( a ) \r\n<type ' dict ' ><\/pre>\n

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

Dictionary comprehension provides a concise way to create a dictionary.<\/p>\n

>>> { x : x**2 for x in ( 2 , 4 , 6 ) } \r\n{ 2 : 4 , 4 : 16 , 6 : 36 }<\/pre>\n

Using built-in function<\/strong><\/p>\n

Dictionary can also be created using a built-in function diet ( ). Consider the following example using diet (), which returns the same dictionary { ” one ” : 1 , ” two ” : 2 , ” three ” : 3 } :<\/p>\n

>>> a=dict ( one=1 , two=2 , three=3 )\r\n>>> b= { ' one ' : 1 , ' two ' : 2 , ' three ' : 3 }\r\n>>> c=dict ( zip ( [ ' one ' , ' two ' , ' three ' ] , [ 1 , 2 , 3 ] ) ) \r\n>>> d=dict ( [ ( ' two', 2), ( ' one ', 1), ( ' three ', 3) J )\r\n>>> e=dict ( { ' three ' : 3 , ' one ' : 1 , ' two ' : 2 } )\r\n>>> a==b==c==d==e \r\nTrue<\/pre>\n

Accessing dictionary elements<\/strong><\/p>\n

To access a dictionary value, use the key enclosed within the square bracket. A KeyError exception is raised if the key is not present in the dictionary.<\/p>\n

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a [ ' guido ' ]\r\n4127<\/pre>\n

Updating dictionary elements<\/strong><\/p>\n

It is possible to add a new item in a dictionary and can also change the value for a given key.<\/p>\n

>>> a= { 'sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> a [ ' mike ' ]=2299 # Add new item\r\n>>> a [ ' guido ' ]=1000 # Update existing item\r\n>>> a\r\n{ ' sape ' : 4139 , ' mike ' : 2299 , ' jack ' : 4098 , ' guido ' : 1000 }<\/pre>\n

It is also possible to update a dictionary with another dictionary using the update ( ) method (discussed later).<\/p>\n

Deleting dictionary elements<\/strong><\/p>\n

To remove a dictionary item (key-value pair) or the entire dictionary, one can use the del statement. To remove all items (resulting in an empty dictionary), the clear ( ) method (discussed later) can be used.<\/p>\n

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 } \r\n>>> del a [ ' guido ' ]\r\n>>> a\r\n{ ' sape ' : 4139 , ' jack ' : 4098 }\r\n>>> del a\r\n>>> a\r\nTraceback ( most recent call last ) :\r\nFile \" <stdin> \", line 1, in <module>\r\nNameError: name ' a ' is not defined<\/pre>\n

Membership operation<\/strong><\/p>\n

Dictionary support membership operation i.e. checking the existence of a key in the dictionary.<\/p>\n

>>> a= { 'sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> ' jack ' in a \r\nTrue\r\n>>> ' tom ' not in a \r\nTrue\r\n>>> 4127 in a \r\nFalse<\/pre>\n

Looping techniques<\/strong><\/p>\n

When looping through the dictionary, the key and corresponding value can be retrieved at the same time using the iteritems ( ) method.<\/p>\n

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }\r\n>>> for k , v in a . iteritems ( ) :\r\n. . . print k , v\r\nsape 4139 \r\njack 4098 \r\nguido 4127<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Dictionary. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Dictionary Dictionary Another useful mutable built-in type is “dictionary”. A dictionary is an unordered group of comma-separated “key: value” pairs enclosed within braces, with the requirement that the keys are …<\/p>\n

Python Programming \u2013 Dictionary<\/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 - 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\/\" \/>\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 - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Python Programming \u2013 Dictionary. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Dictionary Dictionary Another useful mutable built-in type is “dictionary”. A dictionary is an unordered group of comma-separated “key: value” pairs enclosed within braces, with the requirement that the keys are … Python Programming \u2013 Dictionary Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-programming-dictionary\/\" \/>\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-21T05:06:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:22:32+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=\"4 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\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-programming-dictionary\/\",\"name\":\"Python Programming \u2013 Dictionary - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-21T05:06:28+00:00\",\"dateModified\":\"2023-11-10T06:22:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-programming-dictionary\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming \u2013 Dictionary\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Programming \u2013 Dictionary\",\"datePublished\":\"2023-10-21T05:06:28+00:00\",\"dateModified\":\"2023-11-10T06:22:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-dictionary\/#webpage\"},\"wordCount\":402,\"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\/#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 - 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\/","og_locale":"en_US","og_type":"article","og_title":"Python Programming \u2013 Dictionary - Python Programs","og_description":"In this Page, We are Providing Python Programming \u2013 Dictionary. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Dictionary Dictionary Another useful mutable built-in type is “dictionary”. A dictionary is an unordered group of comma-separated “key: value” pairs enclosed within braces, with the requirement that the keys are … Python Programming \u2013 Dictionary Read More »","og_url":"https:\/\/python-programs.com\/python-programming-dictionary\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-21T05:06:28+00:00","article_modified_time":"2023-11-10T06:22:32+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"4 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\/#webpage","url":"https:\/\/python-programs.com\/python-programming-dictionary\/","name":"Python Programming \u2013 Dictionary - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-21T05:06:28+00:00","dateModified":"2023-11-10T06:22:32+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-programming-dictionary\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-programming-dictionary\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-programming-dictionary\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Programming \u2013 Dictionary"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-programming-dictionary\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-programming-dictionary\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Programming \u2013 Dictionary","datePublished":"2023-10-21T05:06:28+00:00","dateModified":"2023-11-10T06:22:32+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-programming-dictionary\/#webpage"},"wordCount":402,"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\/#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\/3374"}],"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=3374"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3374\/revisions"}],"predecessor-version":[{"id":3376,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3374\/revisions\/3376"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}