{"id":228,"date":"2022-09-24T15:14:13","date_gmt":"2022-09-24T07:14:13","guid":{"rendered":"https:\/\/islandtropicaman.com\/wp\/?p=228"},"modified":"2022-10-03T09:44:30","modified_gmt":"2022-10-03T01:44:30","slug":"python-tutorial-chapter-8","status":"publish","type":"post","link":"https:\/\/islandtropicaman.com\/wp\/2022\/09\/24\/python-tutorial-chapter-8\/","title":{"rendered":"Python Tutorial &#8212; Chapter 8"},"content":{"rendered":"<p>In this chapter let us look at the python dictionary collection object. Python dictionary object has both key and value pair and it is used to store data. The key of the python dictionary is not duplicatable.<\/p>\n<p>In order to create the dictionary&#8217;s key and value pair, you need a key that links to a value (data) like below:-<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npower = {\r\n  &quot;station1&quot;: 200,\r\n  &quot;station2&quot;: 300,\r\n  &quot;location&quot;: &quot;New York&quot;\r\n}\r\n<\/pre>\n<p>In order to retrieve the value from a dictionary, used its key as follows:-<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npower&#x5B;&quot;station1&quot;]\r\n<\/pre>\n<p>In order to change the value of a key, all you need to do is as follows:-<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npower&#x5B;'station1'] = 500\r\n<\/pre>\n<p>Example: Loop and print the key and value pairs within the above dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfor key, value in power.items():\r\n   print(key + &quot; : &quot; + str(value))\r\n<\/pre>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nstation1 : 200\r\nstation2 : 300\r\nlocation : New York\r\n<\/pre>\n<p>Example: Loop and print only the values of the above dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfor value in power.values():\r\n   print(value)\r\n<\/pre>\n<p>Example: Get an item from the key.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\na = power.get(&quot;station1&quot;) # 200\r\n<\/pre>\n<p>Example: Get rid of one item in the above dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nprint(power.pop(&quot;station1&quot;))\r\n    print(power)\r\n<\/pre>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n200\r\n{'station2': 300, 'location': 'New York'}\r\n<\/pre>\n<p>Example: Put more items into the above dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npower&#x5B;'station3'] = 600\r\n<\/pre>\n<p>Example: Clear all the key and value pairs in the above dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npower.clear()\r\n<\/pre>\n<p>Example: Print the keys of the above dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfor key in power.keys():\r\n   print(key)\r\n<\/pre>\n<p>Example: Get the key and value pair of the last item in the above dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npower.popitem()\r\n<\/pre>\n<p>Example: Make and assign a copy of the above dictionary to another dictionary.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\npower1 = power.copy()\r\n<\/pre>\n<p>There are still a lot of dictionary methods I have not yet covered and you can learn all of them on the official Python document&#8217;s page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter let us look at the python dictionary collection object. Python dictionary object has both key and value pair and it is used to store data. The key of the python dictionary is not duplicatable. In order to create the dictionary&#8217;s key and value pair, you need a key that links to a&#8230;<\/p>\n<p class=\"more-link-wrap\"><a href=\"https:\/\/islandtropicaman.com\/wp\/2022\/09\/24\/python-tutorial-chapter-8\/\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &ldquo;Python Tutorial &#8212; Chapter 8&rdquo;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,72],"tags":[63],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-python","category-python-tutorial","tag-python-dictionary"],"blog_post_layout_featured_media_urls":{"thumbnail":"","full":""},"categories_names":{"8":{"name":"Python","link":"https:\/\/islandtropicaman.com\/wp\/category\/python\/"},"72":{"name":"Python Tutorial","link":"https:\/\/islandtropicaman.com\/wp\/category\/python-tutorial\/"}},"tags_names":{"63":{"name":"python dictionary","link":"https:\/\/islandtropicaman.com\/wp\/tag\/python-dictionary\/"}},"comments_number":"0","_links":{"self":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":2,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":230,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/posts\/228\/revisions\/230"}],"wp:attachment":[{"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/islandtropicaman.com\/wp\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}