{"id":72,"date":"2022-10-16T23:04:32","date_gmt":"2022-10-16T19:34:32","guid":{"rendered":"http:\/\/zahramansoori.com\/blog\/?p=72"},"modified":"2022-10-16T23:13:03","modified_gmt":"2022-10-16T19:43:03","slug":"what-is-dictionary-in-python","status":"publish","type":"post","link":"https:\/\/zahramansoori.com\/blog\/2022\/10\/16\/what-is-dictionary-in-python\/","title":{"rendered":"What is Dictionary in Python?"},"content":{"rendered":"\n<p>Python has some complicated data structures, such as: <strong>Lists<\/strong>, <strong>Tuples<\/strong>, <strong>Dictionaries <\/strong>and <strong>Sets<\/strong><\/p>\n\n\n\n<p>Dictionary is also called <strong>Associative Array<\/strong>. A dictionary consists of a collection of <strong>key-value pairs<\/strong>.<\/p>\n\n\n\n<p><strong>Definition:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>d = {\n    &lt;key&gt;: &lt;value&gt;,\n    &lt;key&gt;: &lt;value&gt;,\n      .\n      .\n      .\n    &lt;key&gt;: &lt;value&gt;\n}<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Iteration Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>dict = {'color': 'blue', 'fruit': 'apple', 'pet': 'dog'}\nfor key in dict:\n     print(key)<\/code><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>color\nfruit\npet<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for item in dict.items():\n     print(item)<\/code><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>('color', 'blue')\n('fruit', 'apple')\n('pet', 'dog')<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for item in dict.items():\n     print(item)\n     print(type(item))<\/code><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>('color', 'blue')\n&lt;class 'tuple'&gt;\n('fruit', 'apple')\n&lt;class 'tuple'&gt;\n('pet', 'dog')\n&lt;class 'tuple'&gt;<\/code><\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for key, value in dict.items():\n     print(key, '-&gt;', value)<\/code><\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>color -&gt; blue\nfruit -&gt; apple\npet -&gt; dog<\/code><\/code><\/pre>\n\n\n\n<p>References:<\/p>\n\n\n\n<p>[1] <a href=\"https:\/\/realpython.com\/iterate-through-dictionary-python\/\">https:\/\/realpython.com\/iterate-through-dictionary-python\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has some complicated data structures, such as: Lists, Tuples, Dictionaries and Sets Dictionary is also called Associative Array. A dictionary consists of a collection of key-value pairs. Definition: Iteration Code: Output: Code: Output: Code: Output: Code: Output: References: [1] https:\/\/realpython.com\/iterate-through-dictionary-python\/<\/p>\n","protected":false},"author":1,"featured_media":73,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[20,21],"class_list":["post-72","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-dictionary","tag-python"],"_links":{"self":[{"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/posts\/72"}],"collection":[{"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":6,"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":82,"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions\/82"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/media\/73"}],"wp:attachment":[{"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zahramansoori.com\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}