{"id":2938,"date":"2018-03-13T06:58:29","date_gmt":"2018-03-13T06:58:29","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2938"},"modified":"2018-03-14T11:41:58","modified_gmt":"2018-03-14T11:41:58","slug":"python-2-7-fundamentals-collections-dictionaries","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2018\/03\/13\/python-2-7-fundamentals-collections-dictionaries\/","title":{"rendered":"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Dictionaries"},"content":{"rendered":"<p>Just like a set, a dictionary is an unordered collection. A dictionary represents a collection of key:value pairs. In other words, we can use a data type to reference an item instead of just the index of the item. These key:value pairs are separated by commas. Dictionaries are declared in curly brackets. Each item can be a collection item also. Some examples are given below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-3056\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict1-620x186.png\" alt=\"\" width=\"620\" height=\"186\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict1-620x186.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict1-300x90.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict1-678x205.png 678w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict1.png 685w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>In dict2 above, note that we used the key of &#8220;3&#8221; as a string instead of a number. dict3 shows how a dictionary can store collection objects.<\/p>\n<h3>Operators<\/h3>\n<p>The following operators work on dictionaries:<\/p>\n<ul>\n<li>[] &#8211; index operator<\/li>\n<li>in &#8211; search operator<\/li>\n<li>del &#8211; delete operator<\/li>\n<li>len &#8211; length operator. It is more a function than an operator<\/li>\n<\/ul>\n<p><strong>[] &#8211; indexing operator<\/strong><\/p>\n<p>This is used to get the value of a key that exists in the dictionary. If the key does not exist then an error is thrown.<\/p>\n<p><strong>in &#8211; search operator<\/strong><\/p>\n<p>The in operator returns true if a key exists in the dictionary else false<\/p>\n<p><strong>del &#8211; delete dict[key]<\/strong><\/p>\n<p>This deletes a key:value pair from the dictionary. If the key does not exist, an error is thrown<\/p>\n<p><strong>len &#8211; len(dict)<\/strong><\/p>\n<p>This returns the count of items in the dictionary. If there are no items then a length of zero is returned.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-3058\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict2-620x163.png\" alt=\"\" width=\"620\" height=\"163\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict2-620x163.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict2-300x79.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict2.png 689w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Methods<\/h3>\n<p>The following methods are available for dictionaries:<\/p>\n<ul>\n<li>keys &#8211; Returns all the keys in the dictionary<\/li>\n<li>values &#8211; Returns all the values in the dictionary<\/li>\n<li>items &#8211; Returns all the key:value pairs<\/li>\n<li>get &#8211; get the value of a key)<\/li>\n<\/ul>\n<p><strong>keys &#8211; dict.keys()<\/strong><\/p>\n<p>Returns all the keys in the dictionary as a list.<\/p>\n<p><strong>values &#8211; dict.values()<\/strong><\/p>\n<p>Returns all the values int the dictionary as a list<\/p>\n<p><strong>items &#8211; dict.items()<\/strong><\/p>\n<p>Returns all the key:value pairs as a list. Each pair is a list within the main list<\/p>\n<p><strong>get &#8211; dict.get(key, alternative)<\/strong><\/p>\n<p>Gets the value of a key. If a key is not found and alternative is not specified then it returns None. If alternative is specified then it returns the alternative value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-3061\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict3-620x370.png\" alt=\"\" width=\"620\" height=\"370\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict3-620x370.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict3-300x179.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/dict3.png 633w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>In the items method you can see that the pairs are returned as a list with each pair item returned as a list. So using\u00a0 list indexing we can refer to the first pair as list[0] and then the first item within the first pair as list[0][1]<\/p>\n<p>&nbsp;<\/p>\n<p>In the next post we will look at <a href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/14\/python-2-7-fundamentals-basic-input-output\/\" target=\"_blank\" rel=\"noopener\">Basic Input and Output<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Just like a set, a dictionary is an unordered collection. A dictionary represents a collection of key:value pairs. In other words, we can use a <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/13\/python-2-7-fundamentals-collections-dictionaries\/\" title=\"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Dictionaries\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2938","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2938"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2938\/revisions"}],"predecessor-version":[{"id":3080,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2938\/revisions\/3080"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}